Page 1 of 1

Image upload error (error #4?)

Posted: Wed Aug 20, 2008 9:05 am
by JAB Creations
With the following PHP ...

Code: Select all

$uploaddir = 'upload/';
$uploadfile = $uploaddir . basename($_FILES['userfile']['name']);
 
echo '<pre>';
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
    echo "File is valid, and was successfully uploaded.\n";
} else {
    echo "Possible file upload attack!\n";
}
 
echo 'Here is some more debugging info:';
print_r($_FILES);
//echo '<div><b>ERROR:</b>'.$_FILES['userfile']['error'].'</div>';
 
print "</pre>";
...and the following XHTML...

Code: Select all

<form action="profile.php" enctype="multipart/form-data" method="post">
<fieldset>
<legend>Account Picture</legend>
<input accept="image/gif, image/jpeg, image/png" class="text" name="image" type="file" />
<input type="submit" value="upload" />
</fieldset>
</form>
...I get the following output which I do not understand how to interpret.
(
[image] => Array
(
[name] =>
[type] =>
[tmp_name] =>
[error] => 4
[size] => 0
)

)
What is error #4?

Re: Image upload error (error #4?)

Posted: Wed Aug 20, 2008 9:21 am
by onion2k

Re: Image upload error (error #4?)

Posted: Wed Aug 20, 2008 9:25 am
by JAB Creations
Thanks, I found that two seconds after I posted.

Now I have another interesting dilemma...it seems that in Firefox the file only uploads successfully if the file input is in the same fieldset as the submit button. Why is this happening? :roll:

Re: Image upload error (error #4?)

Posted: Wed Aug 20, 2008 9:35 am
by ghurtado
JAB Creations wrote:Thanks, I found that two seconds after I posted.

Now I have another interesting dilemma...it seems that in Firefox the file only uploads successfully if the file input is in the same fieldset as the submit button. Why is this happening? :roll:
No way. It has got to be something else, the fieldset tag couldn't possibly affect that. Can you post the offending HTML?

Re: Image upload error (error #4?)

Posted: Wed Aug 20, 2008 9:43 am
by JAB Creations
After some troubleshooting it looks like you're only allowed to have a single file input per form? That makes no sense however as for example Yahoo mail allows users to upload multiple files.

The issue was that I had two fieldset's (side by side) one for an avatar file upload and another for a profile picture upload. I added more file inputs and it seems like only the last file input would actually send the file.

What do I have to do to have browsers (same result also on Opera) allow two or more file input/uploads for a single form then?

Re: Image upload error (error #4?)

Posted: Wed Aug 20, 2008 9:53 am
by JAB Creations
Gah...durr...RETARD! Same name values for each file input. :banghead: This is why I prefer to stay up until the mid-morning instead of starting to work with code at that time. :roll:

Re: Image upload error (error #4?)

Posted: Wed Aug 20, 2008 10:35 am
by onion2k
:lol:

You're just trying to secure next year's Most Peculiar award aren't you? ;)

Re: Image upload error (error #4?)

Posted: Wed Aug 20, 2008 11:18 am
by JAB Creations
Yes...yes I am. :mrgreen:

Ok so I *hope* this is the last issue: the file seems to upload fine but PHP fails to move the file...thus it gets deleted (as files not moved after being uploaded are deleted from what I've read.)

So...what am I doing wrong here? 8O

Code: Select all

$uploaddir = '/home/jabcreat/public_html/members/upload/';
$uploadfile = $uploaddir . basename($_FILES['userfile']['name']);
 
echo '<pre>';
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
    echo "File is valid, and was successfully uploaded.\n";
} else {
    echo "Possible file upload attack!\n";
}
 
echo 'Here is some more debugging info:';
print_r($_FILES);
 
print "</pre>";
I've set the permissions on the PHP file's directory, the PHP file itself, and the folder to which the uploaded file should be moved to. So I'm not sure what else...I'm slowly going through comments on php.net right now without any luck thus far. Suggestions?

Re: Image upload error (error #4?)

Posted: Wed Aug 20, 2008 12:08 pm
by ghurtado
What is the output of your latest script? You may want to add a check to see if the file exists prior to moving it from the upload temp directory.

As an aside, this line is very interesting:

Code: Select all

   echo "Possible file upload attack!\n";
The only person who would ever see that message would be the potential attacker, wouldn't it? Its like an alarm system that quietly whispers on the burglar's ear "You are currently trying to break into my house". :)

Re: Image upload error (error #4?)

Posted: Wed Aug 20, 2008 12:12 pm
by JAB Creations
It's a copy and paste sort of deal. I learn by taking working scripts and taking them apart. I'm not good with working with things that don't have at least minimally working examples. I'm also not getting any error messages except this with E_ALL...
Notice: Undefined index: userfile in profile.php on line 226
and at that line is the following code...

Code: Select all

$uploadfile = $uploaddir . basename($_FILES['userfile']['name']);
Image array output...
(
[image_1] => Array
(
[name] => avatar_full_size.gif
[type] => image/gif
[tmp_name] => /tmp/phprZPim6
[error] => 0
[size] => 8806
)

)

Re: Image upload error (error #4?)

Posted: Wed Aug 20, 2008 12:20 pm
by ghurtado
The undefined index error indicates that no file was uploaded by that name (thats why E_ALL can be so useful when debugging).

Try changing

Code: Select all

$uploadfile = $uploaddir . basename($_FILES['userfile']['name']);
to

Code: Select all

$uploadfile = $uploaddir . basename($_FILES['image_1']['name']);
I assume "image_1" is the name of your file upload field, since that is the index that is shown when you output the variable.

Re: Image upload error (error #4?)

Posted: Wed Aug 20, 2008 12:25 pm
by JAB Creations
I did that though deleted the upload folder, updated the script and got the following error...
Warning: move_uploaded_file ... failed to open stream: Permission denied
Then I set the permissions to 777 and I saw the file in the FTP! SUCCESS! :-) The lowest permissions this seems to work with is 707. Thanks for the help! I was not making the connection with the <input name="example" /> and $_FILE['example'];