-1

I have an upload form. After the upload form, I have this code in PHP:

$filename    = $_FILES["photo"]["tmp_name"];
$destination = "hr/upload/" . $_FILES["photo"]["name"];
move_uploaded_file($filename, $destination);

$_SESSION['FORM_image'] = $destination;

This code uploads the photo on the destination directory and inserts the path in a session variable.

Now, I want to upload this photo (the uploaded photo) in a MySQL database but my attempts fail.

I tried to use functions like fopen() and similar, but it still not working.

UPDATE: Sorry, perhaps I have not explained well: the upload form and the upload on MySQL are two separated actions. The form uploads the photo on the remote directory. Then I want to "move" this photo from the remote directory to MySQL database.

Can anyone helps me?

Mark Amery
  • 143,130
  • 81
  • 406
  • 459
DearS
  • 182
  • 2
  • 11
  • 1
    What have you tried thus far and do you want to upload the path for the image or the actual bytes ? – yardie Dec 23 '15 at 13:24
  • 1
    Have you try this: http://stackoverflow.com/questions/6472233/can-i-store-images-in-mysql ? – Mirceac21 Dec 23 '15 at 13:25
  • idk if there are any real benefits, but I personally prefer to just store the image data as base64 then just stick it in an image tag. http://php.net/manual/en/function.base64-encode.php – I wrestled a bear once. Dec 23 '15 at 13:27

2 Answers2

0
$myFile = "your file link goes here".
//You open file
$fileResource = fopen($myFile, "r");
//You read it and save data into $fileData variable
$fileData = fread($fileResource, filesize($myFile));
$fileData = stripslashes($fileData);
fclose($fileResource);

After that you insert that into database. Consider that $fileData should be inserted into column that has format -> Blob.

David Demetradze
  • 1,361
  • 2
  • 12
  • 18
  • Sorry, I don't understand well. I have the photo uploaded in the remote folder. I want to "move" this photo from the directory to MySQL db. Thank you!! – DearS Dec 23 '15 at 13:40
  • @L. Soprano I assume that you know how to upload files into folder and now you want to learn how to upload them into database instead of folders. Am I right ? – David Demetradze Dec 23 '15 at 13:42
  • Yes. I know how upload files from html forms to database directly. I don't know how handle the image already uploaded on the remote folder. Thanks – DearS Dec 23 '15 at 13:45
  • @L Soprano Check my code again, I think it will help – David Demetradze Dec 23 '15 at 13:48
0

If you want to store the actual image into the DB , you can use the datatype BLOB for the image field.

If only path , you can use varchar