marcos marcos - 1 year ago 54
PHP Question

Uploading a CSV file to a server

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">

PHP code:

$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.";

Answer Source

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.