0

I don't understand why this is not working. It should be very easy.

upload.php :

$uploaddir = '/usr/share/nginx/www/pitfax/upload/'; 
$uploadfile = $uploaddir . basename($_FILES['userfile']['name']);

echo "<p>";

if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {   
    echo "File is valid, and was successfully uploaded.\n"; 
} 
else {
    echo "Upload failed"; 
}

echo "</p>"; 
echo '<pre>'; 
echo 'Here is some more debugging info:'; 
print_r($_FILES); print "</pre>";

form page:

<form enctype="multipart/form-data" action="upload.php" method="POST">
    <input type="hidden" name="MAX_FILE_SIZE" value="10000000" />
    Send this file: <input name="userfile" type="file" />
    <input type="submit" value="Send File" />
</form>

the error output is :

Upload failed

Here is some more debugging info:Array (
    [userfile] => Array
        (
            [name] => test.pdf
            [type] => application/pdf
            [tmp_name] => /tmp/phptM0p4w
            [error] => 0
            [size] => 1287464
        )

)
  • Webserver : NginX
  • Permissions for upload.php : 644
  • Permissions for upload directory : 755
  • Working in SSH with root user.
  • Edit : upload_max_filesize = 2M
  • Edit : post_max_size = 8M

Another thing! After upload is done, I want to get absolute path of the uploaded file. (ex:/usr/share/nginx/www/pitfax/upload/test.pdf)

8
  • 1
    Have you tried setting the upload directory to 777? Commented Jun 19, 2012 at 21:58
  • @LukePittman Yes it didn't worked. Commented Jun 19, 2012 at 21:58
  • @zerkms What error log are you referring to ? Commented Jun 19, 2012 at 21:59
  • 1
    Are you confident that basename() isn't effecting it? In your debug output perhaps you can echo $uploadfile; and just double check that its not just trying to upload as a directory. Commented Jun 19, 2012 at 22:03
  • 1
    How can $uploadfile be only upload/test.pdf? There should be a full path there! If there is no upload directory at the place where the script is executing, that would explain while move_uploaded_file fails. Commented Jun 19, 2012 at 22:18

1 Answer 1

1

If the value of $uploadfile is upload/test.pdf but you want it to be /usr/share/nginx/www/pitfax/upload/test.pdf than I would suspect that would be your problem.

Try this code:

$uploadfile = '/usr/share/nginx/www/pitfax/upload/' . $_FILES['userfile']['name'];

if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
  echo "File is valid, and was successfully uploaded.\n";
} else {
  print_r ($_FILES);
}

EDIT: Code formatting.

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

3 Comments

Ok the output of echo $uploadfile; is as it should be. But the upload still fails. output is Array ( [userfile] => Array ( [name] => test.pdf [type] => application/pdf [tmp_name] => /tmp/phpbGRbCg [error] => 0 [size] => 1287464 ) )
When trying to help someone else with a problem I was told this isn't a debugging service. So I'm afraid at this point there isn't much I can do. I would check log files (apache error logs) for hints, echo all of your variables, try with fixed file names, etc...
I changed permission of upload directory to 777 again and suddenly the upload worked! Thanks!

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.