linux file upload method to add apache permissions to a file or directory

  • 2020-05-13 04:21:09
  • OfStack

In LAMP environment, test a simple php file upload function, find/var log/apache2 / error log in php warning as follows:

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 25, 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 25, referer: http://localhost/info.html

The code for uploading the file is as follows:


  if (file_exists("upload/" . $_FILES["file"]["name"]))
   {
   echo $_FILES["file"]["name"] . " already exists. ";
   }
  else
   {
    if(move_uploaded_file($_FILES["file"]["tmp_name"], "upload/".$_FILES["file"]["name"]))
        {
            echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
        }
        else
        {
            echo "move error!";
        }
   }

The page says "move error!" , indicating that the move_uploaded_file() function failed to execute.

After searching the information, I found that there was a reason why selinux was started on the Internet, but the system was not started selinux. I checked the default permission of upload directory ls-ld upload, and the result was 755:

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

Change the permissions of upload directory to 777,sudo chmod 777 upload, and then test the upload function successfully. But this method of modifying permissions is not secure.

Therefore, you can change the owner of upload directory to www-data (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


Related articles: