系统环境:
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!";
}
}
页面中显示"move error!",说明move_uploaded_file()函数执行失败。
有说是selinux启动的原因,检查系统并未启动selinux,查看upload目录默认权限ls -ld upload,结果为755:
复制代码 代码示例:
drwxr-xr-x 2 root root 4096 2012-01-31 10:59 upload
将upload目录权限改为777,sudo chmod 777 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