0

I am trying to upload a CSV file to a server with a form but I keep getting a error. Would anyone be able to tell me why the file does not want to upload.

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 code:

<?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.";
    }
?>
marcos
  • 121
  • 3
  • 14
  • 1
  • You never bothered checking if an upload was performed, let alone succeeded. You're simply ASSUMING nothing could ever go wrong. So your move failed because your form has no `name` attribute on the file input, causing no upload to be performed, and $_FILES to never be set. – Marc B Sep 14 '16 at 19:07

1 Answers1

5

You have not given the input a name attribute:

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

Should be:

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

Now $_FILES["fileToUpload"] will be available in php if there are no additional problems.

jeroen
  • 91,079
  • 21
  • 114
  • 132