htmlformsenctype

how do I get HTML inputs in $_POST for a file upload


With the following code in an HTML file, I am able to read in the file but I am not getting the name of the fileToUpload through in the POST. If I remove the ENCTYPE, I do get the fileToUpload through, the problem is that I believe that multipart/form-data is mandatory for a file upload and I I don't use that encopding method, the rest of my routine to process the file goes round in loops.

If there a way to get the name of the user=-selected file through using multipart/form-data?

PS, foo.php has the following code to check the contents of the POST:

<?php
print_r($_POST);
?>

The output with the below HTML code is "Array()", without the ENCTYPE, the output is Array ( [fileToUpload] => energydata2_X.CSV ), the latter is what I need. Thanks for any guidance!

<form action="foo.php" method="POST" ENCTYPE="multipart/form-data">

  Select CSV file to upload:
  
  <input type="file" name="fileToUpload"/> 
  <input type="submit" value="Upload File"/>
  
</form>

Solution

  • Use $_FILES to access the uploaded files.

    https://www.php.net/manual/en/reserved.variables.files.php