Linux file Upload, add Apache permissions to files or directories

Source: Internet
Author: User

System Environment: ubuntu11.10/apache2/php5.3.6

In the lamp environment, when testing a simple php file upload function, you find the following PHP warning appears in/var/log/apache2/error.log:

    1. [Tue Jan 31 09:40:27 2012] [ERROR] [Client 127.0.0.1] PHP Warning:move_uploaded_file (/home/leotody/32883679.jpeg): Failed to open stream:permission denied in/var/www/ upload_file.php on line, referer:http://localhost/info.html
    2. [Tue Jan 31 09:40:27 2012] [ERROR] [Client 127.0.0.1] PHP Warning:move_uploaded_file (): Unable to move '/tmp/phps05ssu ' to '/home/leotody/32883679.jpeg ' In/var/www/upload_ file.php on line, referer:http://localhost/info.html

Prompt without access permission, the file upload code is as follows:

  1. if (file_exists ("upload/". $_files["File" ["Name"]))
  2. {
  3. echo $_files["File" ["Name"]. "already exists.";
  4. }
  5. Else
  6. {
  7. if (Move_uploaded_file ($_files["file"] ["Tmp_name"], "upload/". $_files["File" ["Name"]))
  8. {
  9. echo "Stored in:". " Upload/". $_files["File" ["Name"];
  10. }
  11. Else
  12. {
  13. echo "Move error!";
  14. }
  15. }

"Move error!" is displayed on the page stating that the Move_uploaded_file () function failed to execute.
By looking for information, found that the online is the reason for SELinux to start, check that the system does not start SELinux, view upload directory default permissions ls-ld upload, the result is 755:

    1. Drwxr-xr-x 2 root root 4096 2012-01-31 10:59 upload

Change the upload directory permission to 777,sudo chmod 777 upload, and then test the upload function successfully. However, this method of modifying permissions is not secure.
So you can change the owner of the upload directory for Www-data (that is, Apache), sudo chown-r www-data:www-data upload, and then test the upload function successfully.

      1. Drwxr-xr-x 2 www-data www-data 4096 2012-01-31 10:59 upload

Linux file Upload, add Apache permissions to files or directories

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.