系统环境:ubuntu11.10/apache2/php5.3.6
在LAMP环境中,测试一个简单的php文件上传功能时,发现/var/log/apache2/error.log中出现如下php警告:
- [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
- [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
提示没有访问权限,文件上传的代码如下:
-
- 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!";
- }
- }
通过查找资料,发现网上有说是selinux启动的原因,检查系统并未启动selinux,查看upload目录默认权限ls -ld upload,结果为755:
-
- drwxr-xr-x 2 root root 4096 2012-01-31 10:59 upload
故可以改upload目录的拥有者为www-data(即apache)
- sudo chown -R www-data:www-data upload
-
- drwxr-xr-x 2 www-data www-data 4096 2012-01-31 10:59 upload