简体   繁体   English

为什么我不能在PHP中使用chmod()上传文件?

[英]Why I can not upload files using chmod() in PHP?

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 ... 警告:move_uploaded_file(images / profile / Mordent.jpg)[function.move-uploaded-file]:无法打开流:权限被拒绝...

I have to change scans directory permission to 0777 and then I can upload images. 我必须将scans目录权限更改为0777 ,然后才能上传图像。 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. 我已经在不同的项目中多次使用此代码,但是我对此没有任何问题。

images/scans/ must be writable by the web server user, and should not need to be 0777 , which is world-writable. images/scans/必须是Web服务器用户可写的,并且不必是世界可写的0777 First, images must be readable by the web server user, having a 5 in the last position (like 0755 ). 首先, images必须是Web服务器用户可读的,最后一个位置为5 (如0755 )。 The scans directory must be owned by the web server user or writable by it. scans目录必须由Web服务器用户拥有或可写。 It is preferable to have it owned by the web server user, where its permissions can also be 0755 . 最好由Web服务器用户拥有它,其权限也可以是0755

According to php.net : 根据php.net

Note: This function will not work on remote files as the file to be examined must be accessible via the server's filesystem. 注意:该功能不适用于远程文件,因为必须通过服务器的文件系统访问要检查的文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM