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