Upload file to nginx server through php/html form

8,061

Since $_FILES['fieldname']['errors'] contains 0 (which is UPLOAD_ERR_OK), the actual upload has succeeded. What's failing is the attempt to move the temporary file to the location you specified, which is why move_uploaded_file() is returning false. You're doing something like this:

move_uploaded_file($_FILES['fieldname']['tmp_name'], '/srv/www/uploads')

The most likely problem is that PHP doesn't have write permission to the directory you specified as the second parameter. Find out what user PHP is running as (probably something like www-data), and make sure that that user has write permission:

chown -R www-data /srv/www/uploads
chmod -R u=rwX,g=rwX,o=rX /srv/www/uploads
Share:
8,061

Related videos on Youtube

Dima Deplov
Author by

Dima Deplov

Co-founder of pleeq.com and all related projects, including macilove.com blog with self made blog platform engine. Get in touch with me on Twitter @dimadeplov

Updated on September 18, 2022

Comments

  • Dima Deplov
    Dima Deplov almost 2 years

    I think this is very popular question, however I can't find information I interested in, hope you help me. I new to server administration things, so don't throw stones at me:) I install nginx to my debian virtual server, and already install php, mysql and all I was need to. But when I start use my php scripts I found that I can't upload files through html form.

    I made some test page and I see that, after form was submitted I can see the $_FILES['tmp_name'], but PHP function move_uploaded_file doesn't work. It doesn't moved the file.

    So that's why I start googling and find lot of info about nginx_upload module and so on. Is it posible to config nginx to upload a file, or I must install nginx_upload module to do so?

    What I must to do for allow upload files to my nginx server. Thank you.

    • ceejayoz
      ceejayoz over 11 years
      This likely has more to do with permissions on the directory you're trying to move the files to.
    • Dima Deplov
      Dima Deplov over 11 years
      @ceejayoz where can i find where that temp directory is located? In php.ini or somewhere else?
    • ceejayoz
      ceejayoz over 11 years
      It's not the temporary directory, it's your final destination that matters, which is up to you.
    • mgorven
      mgorven over 11 years
      What's the return value of move_uploaded_file()? Is there a warning printed in the PHP or webserver logs? Does $_FILES['fieldname']['errors'] contain anything?
    • Dima Deplov
      Dima Deplov over 11 years
      @mgorven the return value of move_uploaded_file is false I think (in my code this function is in if state, so i think it false because the else block is executed). $_FILES['fieldname']['errors'] after file loads, contain 0
  • Dima Deplov
    Dima Deplov over 11 years
    where I can set that "upload directory" in nginx? In nginx config file? Or in php.ini I'm really float with it. I found client_body_temp_path directive is it what i'm looking for? thank you for your answer.
  • mgorven
    mgorven over 11 years
    Mode 777 is almost never appropriate.
  • Dima Deplov
    Dima Deplov over 11 years
    Am I get it write: If i make new folder that will allow to save files from form, I need to use this directive (chown and chmod) from command line?
  • mgorven
    mgorven over 11 years
    @flinth Yes, those are example CLI commands.
  • Yogi
    Yogi over 11 years
    But I do agree you don't need the upload module if you don't intend to have progress bar feedback on the uploading
  • Dima Deplov
    Dima Deplov over 11 years
    Okay, I got that, thanks for your replays. I have another question about permissions. When php script running from the web, it runs from user www-data? next, I give permission for user www-data that member of group www-data to write to folder where uploaded files will be stored, and that why we don't give right to 'others' write to a file, am I got it write? Thanks.
  • Dima Deplov
    Dima Deplov over 11 years
    anyway, you answer is work for me, explanation on the question above is desirable :) Thanks.
  • Dan Garthwaite
    Dan Garthwaite almost 11 years
    @mgorven Edited answer to remove 777, but retain simplicity of relying on directory sticky bit.