开发者

Adding For Loop for Multiple File Upload

I currently have this upload script which works perfectly for a single image, but I'm hoping that simply adding a for loop will be able to allow this same script to handle multiple files.

<input type="file" size="20" name="filename">

Here is the current script, I just wasn't able to get any of the for loops I tried to work.

<?php


    $filename = @basename($_FILES['filename']['name']);
    $tmp_filename = $_FILES['filename']['tmp_name'];


    if (!@move_uploaded_file($tmp_filename , DESTINATION_FOLDER . $dest_filename)) {
      $开发者_StackOverflowerrors[] = "Could not upload file (6).";
      break;
    }
?>


OK. now that it's condensed down, to upload multiple files in a standard non-HTML5 fashion, you have to provide multiple file inputs. There's two ways of going about it. Using PHP's array notation so each file input has the same name and gets converted into an array of file data once uploaded, or giving each file input a unique name:

array version:

<input type="file" name="filedata[]" /> 
<input type="file" name="filedata[]" /> 
<input type="file" name="filedata[]" /> 

unique name version:

<input type="file" name="filedata1" />
<input type="file" name="filedata2" />
<input type="file" name="filedata3" />

PHP will build the $_FILES array differently depending on which version you use. For the array side of things, you'll end up with

$_FILES['filedata'] = array(
    'name' => array(
         0 => 'name of first file',
         1 => 'name of second file',
         2 => 'name of third file',
    ), 
    'size' => array(0 => 'size of first file', 1 => 'size of second file', etc...
etc...

Note that each of the files gets its own entry UNDER the individual parameters. If you opt for the unique name version, you'll end up with:

$_FILES['filedata1'] = array('name' => ..., 'size' => ... );
$_FILES['filedata2'] = array('name' => ..., 'size' => ... );
etc...

where each file gets its own dedicated entry in $_FILES.

Regardless of which you go with, all the same file data is present, just arranged differently, which affects how you'd loop over it.


However the approach provided by Marc B. is a good solution (as it supports older browsers as well) there is a more efficient way to solve this problem since the introduction of HTML5. By using the "multiple" tag (http://www.w3schools.com/tags/att_input_multiple.asp) it is a lot faster to let the user select multiple files at once. The only thing that should still be present is the [] in the filename (making the result into an array).

HTML Code:

<input type='file' name='filedata[]' multiple/>

PHP:

if(isset($_FILES)) print_r($_FILES);

Result:

[filedata] => Array
        (
            [name] => Array
                (
                    [0] => images.jpg
                    [1] => logo-2013.jpg
                )
            [type] => Array
                (
                    [0] => image/jpeg
                    [1] => image/jpeg
                )
            [tmp_name] => Array
                (
                    [0] => /tmp/phpWGKNMX
                    [1] => /tmp/phpfIHDpH
                )
            [error] => Array
                (
                    [0] => 0
                    [1] => 0
                )
            [size] => Array
                (
                    [0] => 2521
                    [1] => 76585
                )
        ) 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜