2016-09-14 45 views
0

我想用一個表單上傳一個CSV文件到服務器,但我不斷收到錯誤。任何人都可以告訴我爲什麼該文件不想上傳。上傳一個CSV文件到服務器

HTML代碼:

 <form action="upload.php" method="post" enctype="multipart/form-data"> 
      Select CSV file to upload 
      <input type="file" accept=".csv"> 
      <input type="submit" value="Upload File" name="submit"> 
     </form> 

PHP代碼:

<?php 
$target_dir = "uploads/"; 
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]); 
$fileType = pathinfo($target_file,PATHINFO_EXTENSION); 

// Allow certain file formats 
if($fileType != "csv") { 
    echo "Sorry, only CSV files are allowed. "; 
} 
else { 
    if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) { 
     echo "The file ". basename($_FILES["fileToUpload"]["name"]). " has been uploaded."; 
    } else { 
     echo "Sorry, there was an error uploading your file."; 
    } 
?> 
+1

什麼是錯誤? –

+0

你永遠不會檢查上傳是否被執行,更不用說成功了。你只是假設什麼都不會出錯。所以你的舉動失敗了,因爲你的表單在文件輸入中沒有'name'屬性,導致沒有執行上傳,並且$ _FILES永遠不會被設置。 –

回答

4

您還沒有給出input名稱屬性:

<input type="file" accept=".csv"> 

應該是:

<input type="file" name="fileToUpload" accept=".csv"> 

現在$_FILES["fileToUpload"]將在PHP中可用,如果沒有其他問題。

+1

謝謝!這解決了這個問題。 – marcos