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

问题描述:

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.

我想在远程主机上上传图像(本地一切都正常)。 p> \ n

我使用下面的代码就像我以前的其他项目一样: p>

  if(move_uploaded_file($ _ FILES [“scan”] [“tmp_name”]  ,'images / scans / 1.jpg')){
 \ chmod('images / scans / 1.jpg','0644'); 
} 
  code>  pre> 
 
 但每次我在互联网上运行此代码时,都会收到错误: p> 
 
 

警告:move_uploaded_file(images / profile / Mordent.jpg)[function.move -uploaded-file]: 无法打开流:权限被拒绝... p> blockquote>

我必须更改扫描 code>目录权限 到 0777 strong>然后我可以上传图片。 否则我不能。 这对我来说很奇怪。 我在不同的项目中多次使用过这段代码,我对它没有任何问题。 p> div>

images/scans/ must be writable by the web server user, and should not need to be 0777, which is world-writable. First, images must be readable by the web server user, having a 5 in the last position (like 0755). The scans 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 be 0755.

According to 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.