Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Add <code>[]</code> to the name of the input field, like so:</p> <pre><code>&lt;input type="file" name="uploaded[]" multiple="" /&gt; </code></pre> <p>Each file uses the same name "uploaded", so "uploaded" gets replaced by each successive file that is processed. By adding brackets, each successive file is added to the "uploaded" array instead. Here's the <code>$_FILES</code> array I get after adding <code>[]</code>:</p> <pre><code>Array ( [uploaded] =&gt; Array ( [name] =&gt; Array ( [0] =&gt; file4.txt [1] =&gt; file1.txt [2] =&gt; file2.txt [3] =&gt; file3.txt ) [type] =&gt; Array ( [0] =&gt; text/plain [1] =&gt; text/plain [2] =&gt; text/plain [3] =&gt; text/plain ) [tmp_name] =&gt; Array ( [0] =&gt; C:\temp\php95.tmp [1] =&gt; C:\temp\php96.tmp [2] =&gt; C:\temp\php97.tmp [3] =&gt; C:\temp\php98.tmp ) [error] =&gt; Array ( [0] =&gt; 0 [1] =&gt; 0 [2] =&gt; 0 [3] =&gt; 0 ) [size] =&gt; Array ( [0] =&gt; 7 [1] =&gt; 2850 [2] =&gt; 27 [3] =&gt; 231 ) ) ) </code></pre> <p>Index <code>0</code> in each array is one file, index <code>1</code> in each array is the next file, and so on.</p> <hr> <p>This is also how you would get the values of multiple checkboxes of the same name. (See <a href="http://www.php.net/manual/en/language.variables.external.php#77344" rel="nofollow">this comment</a> for an example.</p> <hr> <p>Here's how you could loop through the uploaded files.</p> <pre><code>// first get the count of how many files are uploaded $numFiles = count(array_filter($_FILES['uploaded']['name'])); for ($i = 0; $i &lt; $numFiles; ++$i) { $target_path = 'c:/temp/' . basename($_FILES['uploaded']['name'][$i]); if(move_uploaded_file($_FILES['uploaded']['tmp_name'][$i], $target_path)) { echo "the file ".basename($_FILES['uploaded']['name'][$i])." has been uploaded&lt;br /&gt;"; } } </code></pre> <p>Notice how I use <code>$i</code> in a <code>for</code> loop to keep track of the index of the current file.</p> <p>(If you're wondering why I called <code>array_filter()</code> to get the count, it looks like a blank entry automatically exists if you don't upload any files. <code>array_filter()</code> removes that blank/invalid entry.)</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload