2012-04-11 66 views
1

我用下面的腳本做一個簡單的文件上傳:PHP簡單的文件上傳

$errors = ''; 
$target_path = "[PATH HERE]"; 

$target_path = $target_path . basename($_FILES['uploadFile']['name']); 

if(move_uploaded_file($_FILES['uploadFile']['tmp_name'], $target_path)) { 
    $errors = "The file ". basename($_FILES['uploadFile']['name']). " has been uploaded"; 
} else{ 
    $errors = "There was an error uploading the file, please try again! Type: " . $_FILES['uploadFile']['type']; 
    } 

出於某種原因,我得到一個錯誤上傳文件和不顯示文件類型。似乎只抓取沒有擴展名的文件名(即「test」而不是「test.pdf」)。我確信這很簡單,但是我做錯了什麼?

+5

由於某些原因,人們堅持從不做適當的錯誤檢查。確保$ _FILES ['uploadFile'] ['error']沒有錯誤,然後確保move_uploaded_file中的路徑正確,然後如果仍然存在問題,請確保Web服務器的用戶正在運行在目錄上寫入權限。 – Corbin 2012-04-11 18:35:29

+1

並檢查'upload_max_filesize' php ini變量是否足夠大。 – mamadrood 2012-04-11 18:37:30

+0

'var_dump($ _ FILES)' – Travesty3 2012-04-11 18:39:12

回答

0

如果您檢查文件數組中的錯誤元素,您可能會發現它的值不是0。如果沒有出錯,錯誤應爲0。否則,將存儲在錯誤中的值與PHP文檔進行比較,以確定出錯的地方。

0

也許你輸入的路徑錯誤(結束斜線)或php沒有權限寫入目錄。

<?php 
error_reporting(E_ALL); // Show some errors 

$target_path = "/var/www/somesite.com/uploads/"; // Would require a ending slash 

$target_path = $target_path.basename($_FILES['uploadFile']['name']); 

if(move_uploaded_file($_FILES['uploadFile']['tmp_name'], $target_path)) { 
    $errors = "The file ". basename($_FILES['uploadFile']['name']). " has been uploaded"; 
} else{ 
    $errors = "There was an error uploading the file, please try again! Type: " . $_FILES['uploadFile']['type']; 
} 

?> 
+0

我再次檢查了路徑,並且結尾的斜槓在那裏。我也檢查了權限,他們也是正確的。 – bdev 2012-04-11 18:58:48