2012-09-08 51 views
2

所以,我讀了this question關於move_uploaded_file()的問題。但是,在我的apache驅動的localhost燈棧上,它的工作很好。所以我認爲它可能是一個文件系統/路徑的東西,而不是代碼的東西。文件上傳/移動溫度不工作nginx

當上傳文件到我的網站本地,它的工作原理。

,但是當我的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);

回答

5

該文件夾有權限等問題。

該路徑中的/files目錄實際上不存在。我以某種方式從未意識到該文件夾​​根本就不在那裏。哎呦。

然後,我必須確定使用什麼用戶nginx的執行PHP:

ps aux | grep "nginx" 

然後我不得不chownfiles目錄:

chown -R root:userFromStep1 files 

然後我不得不chmod目錄:

chmod g+w files 

奏效LIK魅力。

0

能否請給予確切路徑,而不是使用__FILES__嘗試。因爲它不會成爲nginx問題。這將只是路徑問題