2010-10-09 59 views
0

有趣的問題我似乎遇到過。我有一個表單上傳圖像並將其存儲在數據庫表中。表單上傳圖像文件OK並使其可用於處理。問題如下:使用move_uploaded_file到指定的目錄不起作用,但是使用copy()到這個目錄呢。PHP move_uploaded_file/copy issue

的代碼當前如下:

$file = $_FILES['doc_path']; 

    $ext = array_pop(explode('.', $file['name'])); 
    $filename = uniqid() . '.' . $ext; 

    if ($file['error'] == UPLOAD_ERR_NO_FILE && ! strlen($this->filename)) { 
    throw new Exception('Please select a file to upload'); 
    } elseif ($file['error'] == UPLOAD_ERR_NO_FILE) { 
    return true; // already have a file 
    } elseif ($file['error']) { 
    throw new Exception('File upload error'); 
    } elseif (! $file['size']) { 
    throw new Exception('File is of zero length'); 
    } else { 

    $path = 'uploads/' . $filename; 


    if (! move_uploaded_file($file['tmp_name'], $path)) { 
    throw new Exception('Could not upload file'); 
    } 

    return $filename; 
    } 

我已經檢查目標目錄存在,並且該目錄是可寫的。使用move_uploaded_file()不會產生錯誤,只會捕獲「無法上傳文件」異常。

本來想過如果這是一個權限問題,那麼用move_uploaded_file代替copy就行不通了?

+0

你有沒有得到這個修復?我有同樣的問題。 – Scott 2011-07-21 16:04:45

+0

oops,沒有擴展下面的評論線程。我找到了print_r($ _ FILES);對我非常有幫助。謝謝。 – Scott 2011-07-21 17:27:32

回答

0

使用move_uploaded_file()不會產生錯誤,只會捕獲「無法上傳文件」異常。

這可能是由於您的錯誤報告設置。

error_reporting(E_ALL);echo error_get_last();顯示什麼?

+0

嗨,error_get_last報告:陣列([型] => 2048 [消息] =>只有變量應該通過引用傳遞[文件] => /application/modules/admin/forms/Page.php [行] => 211)這是指行$ ext = array_pop ....看不出有什麼不同。 – simnom 2010-10-09 15:26:02

+0

@simnom確實存在'$ file ['tmp_name']'?請參閱手冊:'如果文件名不是有效的上傳文件,那麼不會發生動作,和move_uploaded_file()以將返回FALSE.' – 2010-10-09 15:27:57

+0

的move_uploaded_file語句之前添加回聲$文件[「不對tmp_name」]生產「的/ tmp/phpfyaV0G 「所以我假設是的。 – simnom 2010-10-09 15:30:41

0

@simnon -

我認爲這可能是一個路徑問題。無論是在你的來源還是你的目標。

嘗試改變:

$path = 'uploads/' . $filename; 

要:

$path = '/uploads/' . $filename; 
+0

嗨,不用怕,試過了,沒有什麼區別。 – simnom 2010-10-09 15:25:04