lz刚刚才接触php开发,在这里总结一下自己的一些经验吧
lz在实现把图片上传到服务器端并生成缩略图这个功能的时候,由于涉及到文件的创建,浏览器会报错:
Warning: imagejpeg(): Unable to open 'uploads/image_50/des_big.jpg' for writing: Permission denied in/var/www/html/baotuan/baotuan/test/resizeImage1.php on line 21
Warning: imagejpeg(): Unable to open 'uploads/image_220/des_big.jpg' for writing: Permission denied in/var/www/html/baotuan/baotuan/test/resizeImage1.php on line 22
Warning: imagejpeg(): Unable to open 'uploads/image_350/des_big.jpg' for writing: Permission denied in/var/www/html/baotuan/baotuan/test/resizeImage1.php on line 23
Warning: imagejpeg(): Unable to open 'uploads/image_800/des_big.jpg' for writing: Permission denied in/var/www/html/baotuan/baotuan/test/resizeImage1.php on line 24
仔细一看是因为没有权限,这是因为php的执行者是apache的用户,那么apache的用户是谁呢?
我们可以先在你的php文件里echo exec(‘whoami’);获得文件的执行者,我的执行者是www-data。
在网上搜了好多种方法之后,有说更改文件用户的,由于默认文件用户是root,可以更改成www-data,具体如下
chown www-data -R /var/www/html/文件目录
chmod 775 -R /var/www/html/文件目录
成功!!
另外一种方法:修改该目录所属的组
chgrp www-data -R /var/www/html/文件目录
chmod 775 -R /var/www/html/文件目录
同样成功!!