1

I'm trying to use blueimp's jQuery file upload script.

The files are sent to "upload.php":

if (isset($_FILES['file'])) {

    $file = $_FILES['file'];

    // Set variables
    $file_name  = stripslashes($file['name']);

    if (!is_uploaded_file($file['name'])) {
        echo '{"name":"'.$file_name.' is not a uploaded file."}';
        exit;
    }
}

.. but the script fails at is_uploaded_file despite passing isset($_FILES['file']).

What may be causing this?

EDIT:

I changed from $file['name'] to $file['tmp_name'] and the is_uploaded_file is passed. Now the script fails at move_uploaded_file:

if (move_uploaded_file($file_name, $upload_dir."/".$file_name)) {
    echo "success";
} else {
    echo "failed";
}
1
  • what does var_dump($_FILES['file']); say? Commented Mar 31, 2011 at 9:16

1 Answer 1

3

You should use is_uploaded_file($file['tmp_name']). This is the actual filename on the server.

$file['name'] is the filename on the client's computer, which is only handy for renaming the file after it was uploaded.

For more information, read the docs on is_uploaded_file():

For proper working, the function is_uploaded_file() needs an argument like $_FILES['userfile']['tmp_name'], - the name of the uploaded file on the client's machine $_FILES['userfile']['name'] does not work

Additionally, you said move_uploaded_file() is not working as well. As expected, this is caused by the exact same problem:

You are trying to move the file $file_name, but $file_name is set to $file['name'] and not $file['tmp_name']. Please understand that $file['name'] contains only a string that equals the original filename on the computer, while $file['tmp_name'] contains a string pointing to a path on the server where the filename is not temporarily stored.

Sign up to request clarification or add additional context in comments.

3 Comments

This worked! However, the script now fails at move_uploaded_file. I'll update the question. Hang on...
@Orolin before you do, be sure this is not the exact same problem :)
Yes, you were absolutely right. Thanks for pointing this out Aron :) I was totally confused about the difference between $file['name'] and $file['tmp_name'].

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.