Search code examples
phpjqueryuploadis-uploaded-file

PHP & jQuery file upload issues


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

Solution

  • 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.