Image upload

Hi guys,
I have this code that I use to upload images. It works fine, but when I click on upload with no file inserted I get:

Warning: file_get_contents() [function.file-get-contents]: Filename cannot be empty in /home2/ciudada2/public_html/images.php on line 21

Warning: getimagesize() [function.getimagesize]: Filename cannot be empty in /home2/ciudada2/public_html/images.php on line 23
That’s not an image

How can I make these Warnings disappear, or at least not have the client see them if he clicks upload without inserting a file?

code:

<?php

//connect to databse
include ('connection.php');

//file properties
$file = $_FILES['image']['tmp_name'];

if (!isset($file))
    echo"Please select an image.";
else
  {
    //addslashes helps prevent sql injections
    $image = addslashes(file_get_contents($_FILES['image']['tmp_name']));
    $image_name = addslashes($_FILES['image']['name']);
    $image_size = getimagesize($_FILES['image']['tmp_name']);

    if($image_size==FALSE)
    echo "That's not an image";
          else
          {
            if (!$insert = mysql_query("INSERT INTO images VALUES ('','$image_name','$image')"))
               echo"Problem uploading image";
                            else
                            {
                             $lastid = mysql_insert_id();
                             echo"Image uploaded.<p /> Your image:<p /><img src=getimage.php?id=$lastid>";
                            }
          }

  }

?>

To suppress the errors add a @ to the function:


@getimagesize()

But you should do more than that and at least display an error saying no image selected and end. I have not fully read your code but you could be adding empty records to your database otherwise.

God, I’ve been working on this for 2 hours now and then you come and solve it with one symbol. Such a newb.

Thanks Rubble.

can you tell me why, when using the following code, it keeps on echoing “image too big” no matter what size my image is?


<?php

//connect to databse
include ('connection2host.php');

//file properties
$file = $_FILES['image']['tmp_name'];


if (!isset($file))
    echo"Please select an image.";
else
  {
    //addslashes helps prevent sql injections
    $image = @addslashes(file_get_contents($_FILES['image']['tmp_name']));
    $image_name = addslashes($_FILES['image']['name']);
    $size = @getimagesize($_FILES['image']['tmp_name']);

    if($size==FALSE)
    echo "That's not an image";
          else
          {

                        if ($size > 100000)
                        {
                         echo"image too big";
                        }else
                                  {

            if (!$insert = mysql_query("INSERT INTO images VALUES ('','$image_name','$image')"))
               echo"Problem uploading image";
                            else
                            {
                             $lastid = mysql_insert_id();
                             echo"Image uploaded.<p /> Your image:<p /><img src=getimage.php?id=$lastid height=\\"$height\\" width=\\"$width\\">";
                            }
                                  }
          }

  }

?>

Ok I found my mistake.

I used " if ($size > 100000)" echo “image too big”; but $size= @getimagesize($_FILES[‘image’][‘tmp_name’]);

so I changed it to @getimagesize($_FILES[‘image’][‘size’:blush:]);

can you please give me the code of html file u r using with this to upload data on database… i need it…

Hi Adam,

Just a quick heads up - the mysql functions that you’re using in your script have been depreciated, and will be removed from PHP. You should think about switching to using the [fphp]mysqli[/fphp] or [fphp]PDO[/fphp] extensions instead.

100000 is rather small I would think for image uploading.
http://www.flightpedia.org/convert/100000-bytes-to-megabits.html

You might go with at least 393216.