Avatar of sanchit gupta
sanchit gupta

asked on 

Inserting images in database PDO

I have created a very basic form for inserting data in database PDO. I'm done with the url, name and title but got stucked with the image. How to get image inserted into database through the "folder". I also want to fetch all the details onto some another page.
Any help in styling up these would also be highly appreciated.
<?php
ob_start();
include ("./inc/header.inc.php");
$fileToUpload="";
if(isset($_POST['newpost'])) {
    $urlInput=$_POST['urlInput'];
    $fileToUpload=$_FILES['fileToUpload']['name'];
    $title=$_POST['title'];
    $userid = Login::isLoggedin();
    $filename = $_FILES['fileToUpload']['name'];
    $filetemp = $_FILES['fileToUpload']['tmp_name'];
    $filesize = $_FILES['fileToUpload']['size'];
    $filebasename = basename($_FILES['fileToUpload']['name']);
    $dir="userdata/folder/";
    $finaldir=$dir.$filebasename;
    move_uploaded_file($filetemp,$finaldir);
    DB::query('INSERT INTO links VALUES (\'\', :urlInput, :fileToUpload, :title, NOW(), :userid, 0)', array(':urlInput'=>$urlInput, ':fileToUpload'=>$fileToUpload, ':title'=>$title, ':userid'=>$userid));
}

?>

<form action="submit.php" method="post">
        <p><label><b>URL</b></label><br>
<input type="url" name="urlInput" value="" placeholder="" required><p />
        <p><label><b>Image</b></label><br>
<input type="file" name="fileToUpload" value="" placeholder="" accept="image/*" required><p />
        <p><label><b>Title</b></label><br>
<input type="text" name="title" value="" placeholder="" required><p />
<input type = "submit" name="newpost" value="Submit"/>
</form>

Open in new window

PHP

Avatar of undefined
Last Comment
Ray Paseur

8/22/2022 - Mon