azirion azirion - 3 months ago 39
HTML Question

Change download dialog default on PHP

This is the typical upload code to bring up the dialog box:

<form action="upload.php" method="post" enctype="multipart/form-data">
Select image to upload:
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="submit" value="Upload Image" name="submit">

When the popup window comes up, it shows ALL FILES, is there a way to filter this by any way? (by file extension, by file name, by whatever..) BEFORE the user chooses a file?

Cause until now, the only way I know is to wait until the user chooses whatever file he pleases (since all files are showing) just to filter it later and tell him he made a mistake picking the wrong file format.


PHP does not interact with the user browser in that way, however, you can use HTML to preset the extension of the files by specifying it in the html input. Here's an example for images:

<input type="file" name="fileToUpload" id="fileToUpload" 
   accept="image/x-png,image/gif,image/jpeg" />

Or even:

<input type="file" name="fileToUpload" id="fileToUpload" accept="image/*" />

Read more about it here.

Note: The user may still be able to select files other than the ones you specify, so you need to check the extension server side.