1

我正在使用move_uploaded_file()将图像上传到服务器,但是它给出了通常的错误:

Warning: move_uploaded_file(upload/file.png) [function.move-uploaded-file]: 
failed to open stream: No such file or directory in 
/home/newuser/public_html/model/account.class.php on line 39

Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move       
'/tmp/phpuLkUgE' to 'upload/file.png' in     
/home/newuser/public_html/model/account.class.php on line 39

不是基于权限的,因为我已将文件夹设置为具有 root 访问权限的 777 并ls -l正确显示它。

755 /home/newuser/public_html/model
755 /home/newuser/public_html/model/account.class.php
777 /home/newuser/public_html/upload

PHP线

move_uploaded_file($_FILES["photo"]["tmp_name"], "../upload/file.png");

我认为问题归结为所有者/组设置配置不正确.. 不久前,我将所有网站作为一个帐户中的子域:

/home/olduser/public_html/subdomains/index.html

然后我改变了这个并创建了一个新的用户帐户来更轻松地管理一个单独的网站,只是将文件移动到...

/home/olduser/public_html/subdomains
/home/newuser/public_html/index.html

中的新文件夹/home/newuser现在拥有并分组为,newuser newuser但我认为 php 可能正在运行,nobody olduser所以这可能导致问题?

我可以尝试什么来解决这个问题?

4

2 回答 2

1

Permissions to a particular file / directory don't just apply on the directory itself, but on the whole path leading up to it.

Example:

/home/ - needs 'x' permission (execute)
/home/newuser/ - needs 'x' permission
/home/newuser/public_html/ - needs 'x' permission
/home/newuser/public_html/avatar/ - needs 'wx' permission (execute + write)
于 2012-06-05T02:57:14.720 回答
0

It was kind of touched on indirectly in Silver89's feedback under Jack's Answer, but not outright stated - so I wanted to provide an answer to what helped me with this issue which had me scratching my head for a long time. ;)

The best approach that I have found for the destination of move_uploaded_file() is to use the full absolute path. It can vary based on whether you are on a Unix\Linux server or Windows server, but this should give you the basic idea.

On my Unix server at work, you cannot use "../anything" but have to use the full absolute internal file path of /var/www/html/uploads/imagename.jpg.

So that is why your last test worked for you, Silver89 - because your server was probably trying to upload the image to http://yourservername.com/upload/file.png instead of http://www.yourservername.com/yoursubfolder/upload/file.png. It probably threw out the "../" part altogether and that folder didn't exist on the server.

You can find out what that full path name is by logging onto the server (terminal/ssh etc.) and issuing the 'pwd' command or by using PHP code and echoing the getcwd() command in a stripped php file in the folder where your images will go.

This site is helpful in figuring this out based on your server using different PHP Server Config Checking Functions - See the table midway down. You can simply echo these out to the screen such as:

echo $_SERVER["SCRIPT_FILENAME"]. 

This was a tough one for me so I hope this makes it a little easier for the next person to find - even if this is 8 months old. ;)

于 2013-02-07T22:53:59.060 回答