How can I select and upload multiple files with HTML and PHP using HTTP POST

De openkb
Aller à : Navigation, rechercher

Sommaire

Questions

I have experience doing this with single file uploads using <input type="file">. However, I am having trouble doing uploading more than one at a time.

For example, I d like to be able to select a series of images, then upload them to the server, all at once.

 It would be great to use a single file input control, if possible.  

Does anyone know how to accomplish this? Thanks!

Answers

http://en.wikipedia.org/wiki/File_select#Multiple_file_selection http://en.wikipedia.org/wiki/File_select#Multiple_file_selection

<!doctype html>
<html>
    <head>
        <title>Test</title>
    </head>
    <body>
        <form method="post" enctype="multipart/form-data">
            <input type="file" name="my_file[]" multiple>
            <input type="submit" value="Upload">
        </form>
        <?php
            if (isset($_FILES[ my_file ])) {
                $myFile = $_FILES[ my_file ];
                $fileCount = count($myFile["name"]);

                for ($i = 0; $i < $fileCount; $i++) {
                    ?>
                        <p>File #<?= $i+1 ?>:</p>
                        <p>
                            Name: <?= $myFile["name"][$i] ?><br>
                            Temporary file: <?= $myFile["tmp_name"][$i] ?><br>
                            Type: <?= $myFile["type"][$i] ?><br>
                            Size: <?= $myFile["size"][$i] ?><br>
                            Error: <?= $myFile["error"][$i] ?><br>
                        </p>
                    <?php
                }
            }
        ?>
    </body>
</html>

Here s what it looks like in Chrome after selecting 2 items in the file dialog:

<img src="https://i.stack.imgur.com/Dg5TW.png" alt="chrome multiple file select">

And here s what it looks like after clicking the "Upload" button.

<img src="https://i.stack.imgur.com/3Q140.png" alt="submitting multiple files to PHP">

http://php.net/manual/en/features.file-upload.php http://php.net/manual/en/features.file-upload.php

Source

License : cc by-sa 3.0

http://stackoverflow.com/questions/1175347/how-can-i-select-and-upload-multiple-files-with-html-and-php-using-http-post

Related

Outils personnels
Espaces de noms

Variantes
Actions
Navigation
Outils