0

I'm trying to upload image to mysql table. When image selected it's working fine. But without selecting image it's display error message. How can i fix it?

Warning: file_get_contents(): Filename cannot be empty in C:\wamp\www\firstdialnew\firstdial\adhandler.php on line 23

$stype=$_POST['stype'];
$image = addslashes(file_get_contents($_FILES['image']['tmp_name']));
$image_name = addslashes($_FILES['image']['name']);

if($stype="")
{
echo "Please fill all the details";
}else{
$sql=mysql_query("INSERT INTO ads
                            (sid,stype,image,image_name)
                            VALUES(NULL,'$stype','$image','$image_name')");
echo "Details Successfully send";
}
5
  • The message is clear,you have nothing in $_FILES,show the part where you upload the file Commented Sep 26, 2014 at 9:59
  • have you tried dumping the contents of your $_FILES array - it seems you have nothing in the array thats why you are getting that message Commented Sep 26, 2014 at 9:59
  • It's a better idea to keep the image in server and store the path in database. Commented Sep 26, 2014 at 10:01
  • There are several errors. if case is incorrect; use of deprecated mysql_* functions; use of $_FILES's tmp_name; also, you didn't move your file from temp folder to final path. Commented Sep 29, 2014 at 1:56
  • And you shouldn't save the image contents in MySQL; save the path only. Commented Sep 29, 2014 at 2:00

5 Answers 5

3

you need to check/validate that image is upload or not try something like that

if (!empty($_FILES['image']['name'])) {
   $image = addslashes(file_get_contents($_FILES['image']['tmp_name']));
   $image_name = addslashes($_FILES['image']['name']);
}else{
   $image = '';
   $image_name = '';
}
Sign up to request clarification or add additional context in comments.

1 Comment

No. You shouldn't use file_get_contents() on $_FILES.
1

Follow my experience, you should save image into folder (ex : public/upload/images/image_name.png) AND save image path into database. That is simple and database's size is not large

1 Comment

Although your suggestion is perfectly correct, this is not an answer (a comment instead).
1

You are not checking for $_FILES try this

$stype=$_POST['stype'];
if(isset($_FILES['image']) ) {
    $image = addslashes(file_get_contents($_FILES['image']['tmp_name']));
    $image_name = addslashes($_FILES['image']['name']);
}else{
    $image = '';
    $image_name = '';
}

I will suggest you to upload file in a folder and store the file name in mysql table.

Here is how you can do that

$file_path = "uploads/";

if(isset($_FILES['image']) ) {
   $file_name  = basename( $_FILES['uploaded_file']['name']);
   $file_path = $file_path . $file_name;

   //You should validate the image before upload
   move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $file_path);
 }else{
    $image = '';
    $image_name = '';
}

//now store file name in db
$sql=mysql_query("INSERT INTO ads
                        (sid,stype,image,image_name)
                        VALUES(NULL,'$stype','$file_name','$image_name')");

2 Comments

what is the different in your answer
@RamSharma this is the suggested use of $_FILES. file_get_contents() is unnecessary & move_uploaded_file() should be used.
0

Did you add :

    <form method="POST" enctype="multipart/form-data">

Into your form (enctype="multipart/form-data")

1 Comment

I feel he is adding this because he is able to save the image but getting issue if image is not selected
0

try this :

 $stype=$_POST['stype'];
     $image_name ='';
     $image='';
     if(!empty($_FILES['image'])) {
     $image = addslashes(file_get_contents($_FILES['image']['tmp_name']));
     $image_name = addslashes($_FILES['image']['name']);
      } 
      if($stype="")
      {
      echo "Please fill all the details";
      }else{
       $sql=mysql_query("INSERT INTO ads
                        (sid,stype,image,image_name)
                        VALUES(NULL,'$stype','$image','$image_name')");
         echo "Details Successfully send";
       }

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.