-1

Whenever I try to upload a image then it shows me this error

Warning: file_get_contents(): Filename cannot be empty

My code is:

<?php
$ImageName = $_FILES["file"]["name"];
$ImageType = $_FILES["file"]['type'];
$ImageSize = $_FILES["file"]['size'];
$ImgData   = addslashes(file_get_contents($_FILES['file']['tmp_name']));
$sql       = "insert into image(imagename,imagesize,imagetype,imagecontent) values('{$ImageName}','{$ImageSize}','{$ImageType}','{$ImgData}')";
$result    = mysql_query($sql);
if ($result) {
    echo "Success";
} else {
    echo "Fail";
}
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
Shankar
  • 9
  • 1
  • 2
  • 2
    `var_dump($_FILES);` what do you get? – aynber Jan 03 '19 at 14:00
  • Are you sure there's a file in `$_FILES['file']['tmp_name']`? Try to dump it like @aynber said – Joppe De Cuyper Jan 03 '19 at 14:01
  • Your code uses the totally outdated and long deprecated mysql extension that actually does not exist any more in current php versions. You really should upgrade. – arkascha Jan 03 '19 at 14:01
  • Saving images itself in the database will make it very large, hard to backup and may occupy memory from the MySQL buffer pool. I prefer to save them on disk – Accountant م Jan 03 '19 at 14:04

1 Answers1

1

Read your error message carefully, all information needed is in there :)

file_get_contents(): Filename cannot be empty means, that the "thing" that you gave the function is "empty" and it should not be "empty". So, what are you inserting into that function?

Correct. $_FILES['file']['tmp_name'] is empty. So somewhere in your other code you have something that's not correct, or something is misspelled.

If you encounter such message, do a log/print of that variable or of the whole array. Then you will see that there is no string in this part of the array.

mhombach
  • 196
  • 11