php $_FILES error code returning an unexpected error

Posted by EmmyS on Stack Overflow See other posts from Stack Overflow or by EmmyS
Published on 2010-05-31T00:43:04Z Indexed on 2010/05/31 0:52 UTC
Read the original article Hit count: 380

Filed under:

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;
        }
    }

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?

© Stack Overflow or respective owner

Related posts about php