Page 1 of 1

Uploading an image

Posted: Mon Mar 03, 2003 10:26 am
by WillowFae
I have some script to upload an image from a users computer via a form. It works fine if you choose to upload an image but if you don't it falls over despite a check to see if an image is there. Uploading is optional for the user so it needs to cope with no image there.

The code I have is as follows

if ($_FILES['img1'] != "")
{
copy($_FILES['img1']['tmp_name'], "images/userpics/".$_FILES['img1']['name']) or die("Couldn't copy the file!");
$image = "images/userpics/".$_FILES['img1']['name'];
}
else
{
// if $_FILES['img1'] was empty, die and let us know why
die("No input file specified");

}


The error message I am getting is

"Warning: Unable to open 'none' for reading: No such file or directory in /home/sites/site289/web/willowfae/memorial/addconfirm.php on line 15
Couldn't copy the file!"

I tried printint the value of $_FILES['img1'] and sure enough it came up as 'none'. However when I change the if statement to read

if ($_FILES['img1'] != "none")

I still get the error.

The else statement is called if I go directly to the page without going through the form.

Any thoughts on what is wrong?

Posted: Mon Mar 03, 2003 10:49 am
by pootergeist
if($_FILES['img1']['tmp_name'] !== "")
{
// we have an upload
}


might do better.
I'd have expected echo $_FILES['img1'] to return 'Array' - strange.

Posted: Tue Mar 04, 2003 3:22 am
by WillowFae
Thanks. That seems to work if I put !=="none". I'll check it out fully over lunch.