Multi file upload with PHP/Javascript and no flash

前端 未结 4 850
傲寒
傲寒 2021-02-10 14:27

I\'m trying to make a webpage that allows the uploading of multiple files at the same times. I will limit the file extensions to the most common images like JPG, JPEG, PNG and G

相关标签:
4条回答
  • 2021-02-10 15:04

    Here is the algorithm:

    You add the new file input fields to your form. Each of this field MUST have a unique name. Then, on the server side, you loop through the $_FILES array looking how many files have been uploaded and handling them.

    0 讨论(0)
  • 2021-02-10 15:14

    You can keep adding 'file' inputs but use a name of something like 'upload[]'

    <input type="file" name="upload[]">
    

    Then in $_FILES['upload'] you will have an array of files you can loop over like

    foreach ($_FILES['upload'] as $file) {
        echo $file['size'];
    }
    
    0 讨论(0)
  • 2021-02-10 15:26

    In using JavaScript to add new upload fields, you could also have JavaScript update some "hidden" input field with the number of upload fields in the form. That way, once you click Submit, that hidden value should be submitted and it will be trivial to parse out the $_FILES array for all the uploaded files.

    0 讨论(0)
  • 2021-02-10 15:30

    If you have a reasonable maximum limit on the number of files, it's probably better to include that many file upload fields in the static form, then use the JavaScript to hide the superfluous ones, than to create them all dynamically. Then the form can still work when JavaScript is unavailable.

    Side note: technically, single HTML file upload forms are already multiple file upload forms. According to the HTML form encoding standard, one should be able to select multiple files in the Browse dialogue box, and they'd be submitted as a multipart/mixed MIME structure.

    However, almost nothing actually supports this. Older versions of Opera do on the client-side (and, I think, an ancient test browser of some sort, possibly Viola), and a few form-parsing components on the server-side, but not the PHP built-ins. In any case the UI is not very usable, so you're not missing much.

    0 讨论(0)
提交回复
热议问题