将CSV文件上载到服务器

时间:2016-09-14 19:02:38

标签: php html

我正在尝试将CS​​V文件上传到带有表单的服务器,但我一直收到错误消息。有人能够告诉我为什么文件不想上传。

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.";
    }
?>

1 个答案:

答案 0 :(得分:5)

您尚未向input提供名称属性:

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

应该是:

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

如果没有其他问题,现在$_FILES["fileToUpload"]将在php中可用。