0

I would like to have an upload form that automatically submits as soon as the user has selected a file.
This question has been asked many times before. If I understood the other threads correctly, this should work:

<form method="post" enctype="multipart/form-data">
  <input id="fileToUpload" onchange="form.submit()" type="file"/>
</form>

<?php 
  if(isset($_FILES["fileToUpload"])){
    echo "You successfully entered a file for the upload!";
    // move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], "uploads/my_image.png");
  }
?> 

When I select a file, I see the file name briefling flashing instead of the "no file chosen". But $_FILES is not set.

Do modern browsers block onchange="form.submit()" for type="file"?
I tried it with Firefox 68 and Microsoft Edge 84.

Snoeren01
  • 69
  • 7

2 Answers2

1

To allow $_FILES to detect your uploaded file, you need name attribute in your input tag. So change

<input id="fileToUpload" onchange="form.submit()" type="file"/>

to

<input id="fileToUpload" name="fileToUpload" onchange="form.submit()" type="file"/>
Muhammad Dyas Yaskur
  • 4,300
  • 8
  • 27
  • 48
1

Form id values are not submitted by default. Only name values are. Try changing id to name in the input tag, e.g.:

<input name="fileToUpload" onchange="form.submit()" type="file"/>

Alan
  • 628
  • 6
  • 12