2

所以,我读过这个关于 move_uploaded_file() 问题的问题。但是,在我的 apache 驱动的 localhost 灯堆栈上,它工作得很好。所以我认为这可能是文件系统/路径的事情,而不是代码的事情。

在本地将文件上传到我的网站时,它可以工作。

但是当我在 QA 服务器上(由 nginx 驱动)时,我收到了这个错误:

2012/09/08 15:34:21 [error] 11794#0: *5187 FastCGI sent in stderr: "files not empty
PHP Warning:  move_uploaded_file(/var/www/qa.mysite.com/mysite/app/files/maps-aprilfools.tiff): failed to open stream: No such file or directory in /var/www/qa.mysite.com/mysite/app/models/files.php on line 516
PHP Warning:  move_uploaded_file(): Unable to move '/tmp/phpvdtznP' to '/var/www/qa.mysite.com/mysite/app/files/maps-aprilfools.tiff' in /var/www/qa.mysite.com/mysite/app/models/files.php on line 516" while reading response header from upstream, client: 72.xxx.xxx.xxx, server: qa.mysite.com, request: "POST /projects/3/files HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "qa.mysite.com", referrer: "http://qa.mysite.com/projects/3/files"

这是我为处理上传文件而编写的代码:

public function fileUploadToProject( $project_id ) {
    if ($_FILES["upload-file"]["error"] > 0) {
        echo "Error: " . $_FILES["file"]["error"] . "<br />";
    } else {

        $dbSuccess      = false;

        $tmp_name       = $_FILES["upload-file"]["tmp_name"];
        $name           = $_FILES["upload-file"]["name"]; // someFile.ext
        $size           = $_FILES['upload-file']['size']; //size in bytes
        $mime           = $_FILES['upload-file']['type']; //size in bytes

        $destination    = __FILES__.'/'.$name;
        $uploaded       = move_uploaded_file( $tmp_name, $destination );

        // add entry to database.

        /*
         * null because there is no container yet.
         * We're only uploading to local
         */
        $user_container_name = null; 

        $uploaded_by = LoggedInUser::get_user_id();

        /*
         * Set this 1 when we're not dealing with our external fileserver
         */
        $isLocal = 1;

        /*
         * Probably shouldn't do this forever for storage size reasons, but for now its useful.
         */
        $localPath = $destination;

        $task_id = null;

        if( $uploaded ) {
        $dbSuccess = $this->insertFileRefService( $task_id, 
                                                  $project_id,
                                                  $user_container_name, 
                                                  $name,
                                                  $mime, 
                                                  $size,
                                                  $uploaded_by,
                                                  $isLocal,
                                                  $localPath
                );
            if($dbSuccess) {
                return true;
            } else {
                // should I rollback / delete that file?
                return false;
            }
        } else {
            return false;
        }

    }
}

那么,关于在 nginx 上将临时文件移动到我的文件系统,我应该知道什么吗?还是您认为这只是一个路径问题?代码问题?

另外,请注意第 516 行是这样的:$uploaded = move_uploaded_file( $tmp_name, $destination );

4

2 回答 2

5

除其他外,该文件夹存在权限问题。

/files路径中的目录实际上不存在。我不知何故从未意识到该文件夹​​根本不存在。哎呀。

然后,我必须确定 nginx 使用什么用户来执行 php:

ps aux | grep "nginx"

然后我不得不到chown目录files

chown -R root:userFromStep1 files

然后我不得不到chmod目录:

chmod g+w files

这就像一个魅力。

于 2012-09-08T17:25:07.107 回答
0

请尝试给出确切的路径而不是使用__FILES__. 因为它不会是 nginx 的问题。这将只是路径问题

于 2012-09-08T16:24:28.450 回答