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:
- [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
- [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:
- 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!";
- }
- }
"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:
- 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.
- 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