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";
}
o01
  • 5,191
  • 10
  • 44
  • 85

1 Answers1

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.

Aron Rotteveel
  • 81,193
  • 17
  • 104
  • 128
  • This worked! However, the script now fails at move_uploaded_file. I'll update the question. Hang on... – o01 Mar 31 '11 at 09:20
  • 1
    @Orolin before you do, be sure this is not the *exact* same problem :) – Aron Rotteveel Mar 31 '11 at 09:23
  • 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']. – o01 Mar 31 '11 at 09:37