php $_FILES error code returning an unexpected error
I have some code that allows users to upload multiple files at once. It was never getting to a specific point after the upload, so I put in an echo to test for the value of the error code, and it's returning a value that I'm not sure I understand. Here's the code:
$tmpTarget = PCBUG_UPLOADPATH;
foreach ($_FILES["attachments"]["error"] as $key => $error) {
if ($error == UPLOAD_ERR_OK) {
$tmp_name = $_FILES["attachments"]["tmp_name"][$key];
$name = str_replace(" ", "_", $_FILES["attachments"]["name"][$key]);
move_uploaded_file($tmp_name, "$tmpTarget/$name");
@unlink($_FILES["attachments"]["tmp_name"][$key]);
}
else {
$errorFlag = true;
echo "error = $error";
exit;
}
开发者_JS百科 }
The code that creates the attachments field looks like this:
for($i=1; $i<=$max_no_img; $i++){
echo "<input type=file name='attachments[]' class='bginput'><br />";
}
where $max_no_img
is a variable set further up in the code, and PCBUG_UPLOAD
path is a constant defined in an included file.
Here's what's confusing: after I submit my form, I go and look in my uploads directory, and the files I've selected through the form are there - they uploaded correctly. However, the code is jumping into the else clause and $error is returning 4, which the php manual indicates means that no file was uploaded.
Any ideas? The files very clearly are getting where they're supposed to. Is there some other definition of "uploaded" that isn't happening?
That error happens when the browser does not send a file, but also when the filename of the sent file is interpreted by PHP as an empty string (see main/rfc1867.c).
Try to force a charset on the form, like this:
<form accept-charset="utf-8" enctype="multipart/form-data" method="post" action="dest.php">
If it doesn't work, sniff the HTTP request where the files are sent (e.g. with wireshark) and post the results. Try also another browser.
EDIT: Your browser is sending only one file. UPLOAD_ERR_NO_FILE
doesn't mean "no file at all was uploaded". If $_FILES["attachments"]["error"][$n] == UPLOAD_ERR_NO_FILE
it means "no file was uploaded for $n
-th file input".
Content-Type: multipart/form-data; boundary=---------------------------3764294497346
Content-Length: 4113
-----------------------------3764294497346
Content-Disposition: form-data; name="subject"
Ritz Camera Club Presentation
-----------------------------3764294497346
Content-Disposition: form-data; name="meeting_date"
May 2010
-----------------------------3764294497346
Content-Disposition: form-data; name="posted_by"
esthermstrom
-----------------------------3764294497346
Content-Disposition: form-data; name="body"
sdjflksjdflsjf
-----------------------------3764294497346
Content-Disposition: form-data; name="attachments[]"; filename="Log.txt"
Content-Type: text/plain
contents of the file snipped
-----------------------------3764294497346
Content-Disposition: form-data; name="attachments[]"; filename=""
Content-Type: application/octet-stream
-----------------------------3764294497346
Content-Disposition: form-data; name="attachments[]"; filename=""
Content-Type: application/octet-stream
-----------------------------3764294497346
Content-Disposition: form-data; name="attachments[]"; filename=""
Content-Type: application/octet-stream
-----------------------------3764294497346
Content-Disposition: form-data; name="submit"
Post Writeup
-----------------------------3764294497346--
精彩评论