为什么我无法在 PHP 中使用 chmod() 上传文件?
我想在远程主机上上传图像(本地一切正常)。
我像以前的其他项目一样使用了下面的代码来执行此操作:
if (move_uploaded_file($_FILES["scan"]["tmp_name"], 'images/scans/1.jpg')) {
chmod ('images/scans/1.jpg','0644');
}
但是每次我在互联网上运行此代码时,都会收到错误:
警告:move_uploaded_file(images/profile/Mordent.jpg) [function.move-uploaded-file]: 无法打开流:权限被拒绝...
我必须将 scans
目录权限更改为 0777,然后才能上传图像。否则我不能。这对我来说很奇怪。我在不同的项目中多次使用过这段代码,并且没有任何问题。
I want to upload an image on a remote host (Everything is ok locally).
I've used the code below like my other previous projects to do it:
if (move_uploaded_file($_FILES["scan"]["tmp_name"], 'images/scans/1.jpg')) {
chmod ('images/scans/1.jpg','0644');
}
But every time I run this code in the internet, I get an error:
Warning: move_uploaded_file(images/profile/Mordent.jpg) [function.move-uploaded-file]:
failed to open stream: Permission denied in ...
I have to change scans
directory permission to 0777 and then I can upload images. Otherwise I can not. It's very strange for me. I've used this code in many times in different projects and I had not any problem with it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
images/scans/
必须可由 Web 服务器用户写入,并且不需要是0777
,这是全局可写的。首先,images
必须可由 Web 服务器用户读取,最后位置有一个5
(如0755
)。scans
目录必须由 Web 服务器用户拥有或可写。最好由 Web 服务器用户拥有它,其权限也可以是0755
。images/scans/
must be writable by the web server user, and should not need to be0777
, which is world-writable. First,images
must be readable by the web server user, having a5
in the last position (like0755
). Thescans
directory must be owned by the web server user or writable by it. It is preferable to have it owned by the web server user, where its permissions can also be0755
.根据 php.net:
According to php.net: