sreethar raja
sreethar raja

Reputation: 25

while uploading a image using php and mysql: Warning: file_get_contents(): Filename cannot be empty

when i am trying to upload a image to photos folder i am having this type of warning msg i dont know what to.. This is my php file

if (!isset($_FILES['image']['tmp_name'])) {
    echo "";
    }else{
    $file=$_FILES['image']['tmp_name'];
    $image= mysql_real_escape_string(addslashes(file_get_contents($_FILES['image']['tmp_name'])));
    $image_name= addslashes($_FILES['image']['name']);

            move_uploaded_file($_FILES["image"]["tmp_name"],"photos/" . $_FILES["image"]["name"]);

            $location="photos/" . $_FILES["image"]["name"];

            $save=mysql_query("insert into add values('$location')") or die("can not insert");
            exit();                 
    }

This is my html code

<form method="POST" action='ap.php' enctype="multipart/form-data">
<input name="image" id="image" type="file" />
<input type='submit' name='Add' value='Add'  />
</form>

Upvotes: 0

Views: 1303

Answers (5)

sreethar raja
sreethar raja

Reputation: 25

After removing these two lines. It works fine

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

Upvotes: 0

Atulk
Atulk

Reputation: 23

Your code working fine, Please check for permission as well for 'photos' must be writable. You can also remove following line as it's have no effect.

$image= mysql_real_escape_string(addslashes(file_get_contents($_FILES['image']['tmp_name'])));

Please paste full code as well may be some issue with your connection string or other code.

Upvotes: 1

Yuva Raj
Yuva Raj

Reputation: 3881

You don't need to escape thing. Just do like,

if (!isset($_FILES['image']['tmp_name'])) {
    echo "Image not selected ";
  }

else
{
    $file=$_FILES['image']['tmp_name']; // temporary name
    $image_name= $_FILES['image']['name']; // original file name

     move_uploaded_file($_FILES["image"]["tmp_name"],"photos/" . $_FILES["image"]["name"]);

     $location="photos/" . $_FILES["image"]["name"];
     $query = "Write your query here...";
     $save=mysqli_query($connection, $query);
     if($save)
      {
        // success...do whatever you want
       }
     else
      {
         // executes when save fails
       }      
    }

WARNING :

mysql_ is deprecated. Migrate to mysqli_ or PDO. Your code is vulnerable to SQL Injection.

Upvotes: 1

Sougata Bose
Sougata Bose

Reputation: 31749

No need of escaping the file name & tmp_name. Simply do -

move_uploaded_file($_FILES["image"]["tmp_name"],"photos/" . $_FILES["image"]["name"]);

$location="photos/" . $_FILES["image"]["name"];

$save=mysql_query("insert into add values('$location')") or die("can not insert");
exit();

Upvotes: 0

Iffi
Iffi

Reputation: 588

No need the following lines:

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

Just removed it and test it will work. You are checking the first if condition its enough.

Upvotes: 1

Related Questions