0

I am trying to store an image from a user in the database but after insertion it is always showing the size of an image as 26B even if the image size was in Kb. I have taken longBlob in MySQL for storing the same. here is my code

<input type="file"  name="profile_picture" id="imgupload"  accept=".jpg,.png,.jpeg">


// for uploading image
   $ff_NAME = $_FILES['profile_picture']['name'];
   $ff_TMP_NAME = $_FILES['profile_picture']['tmp_name'];
   $ff_SIZE = $_FILES['profile_picture']['size'];
   $ff_ERROR = $_FILES['profile_picture']['error'];
   $ff_TYPE = $_FILES['profile_picture']['type'];

   $ffileExt = explode('.',$ff_NAME);
   $ffileActualExtention = strtolower(end($ffileExt));
   $fallowed = array('png','jpeg','jpg');

   if(in_array($ffileActualExtention,$fallowed))
   {
       if($ff_ERROR===0)
       {  
           if($ff_SIZE<500000)
           {
               $ffileNameNew = uniqid('',true)."".$ffileActualExtention;
               $ffileDestination = 'profileuploads/'.$ffileNameNew;
               move_uploaded_file($ff_TMP_NAME,$ffileDestination);
           }
           else
           {
               echo 'File size must be less than 500 kb.';
           }
       }
       else
       {echo "Error in uploading the files";}
   }
   else
   {echo "Only 'png','jpeg','jpg' are allowed";}

Insert query declaration:

  $query = "INSERT INTO detail (`f_profile_picture`) VALUES (?)";
   $stmtt = $conn->prepare($query);//prepared statement method 
   $stmtt->bind_param("s",$ffileNameNew);//binding a parameter to question mark
$stmtt->execute();
   if($stmtt->affected_rows > 0)
     {
     echo("success");
     }

     else
      {
     echo("failed");
     }
Jenea Vranceanu
  • 4,530
  • 2
  • 18
  • 34
joshua
  • 177
  • 1
  • 12
  • 1
    You're not inserting the file, you're inserting the name of the file. You're storing the file in your filesystem (under `profileuploads`) – Nick Jun 16 '20 at 07:44
  • what should i do so that image is stored in database as well as under folder profileuploads?? – joshua Jun 16 '20 at 07:46
  • 2
    Why would you want to have 2 copies of it? Generally storing the image on the file system is the right thing to do. See https://stackoverflow.com/questions/3748/storing-images-in-db-yea-or-nay – Nick Jun 16 '20 at 07:48
  • actually i want to display user its image after he submit the form – joshua Jun 16 '20 at 07:48
  • 1
    If the file is stored in the HTML tree you can just provide a URI to it e.g. `` – Nick Jun 16 '20 at 07:50
  • so it means that there is no need of insert query?? – joshua Jun 16 '20 at 07:52
  • 1
    You do still need to store the filename so that you can create that URI in the future. – Nick Jun 16 '20 at 07:53
  • can you provide some refrence or modify the code because i am not able to get it – joshua Jun 16 '20 at 07:54

1 Answers1

1
  1. move_uploaded_file($ff_TMP_NAME,$ffileDestination) means put uploaded file in your file system of path $ffileDestination
  2. $stmtt->bind_param("s",$ffileNameNew) here you are binding the saved filename
  3. when $stmtt->execute(), the filename is stored in database

now you are trying to show the image right?

  1. select out the stored filename in database
  2. get the path of this image which is stored in your file system
  3. show the image

when you show the image, there are basically two ways:

  1. the file is public, which means you can get the image from url like http://example.com/xxx.png. you can simply use <img src='http://example.com/xxx.png'>
  2. the file is private, you should use php to read the file content and render it out. (base64_encode may help)
Gavin Kwok
  • 98
  • 6