我正在尝试将CSV文件上传到带有表单的服务器,但我一直收到错误消息。有人能够告诉我为什么文件不想上传。
HTML code:
<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.";
}
?>
答案 0 :(得分:5)
您尚未向input
提供名称属性:
<input type="file" accept=".csv">
应该是:
<input type="file" name="fileToUpload" accept=".csv">
如果没有其他问题,现在$_FILES["fileToUpload"]
将在php中可用。