0

My question is, how and will <input type='file' name='file[]'> multiple of theese work in php?

Thanks

Raimonds
  • 2,606
  • 3
  • 20
  • 25
  • possible duplicate of [Multiple file upload in php](http://stackoverflow.com/questions/2704314/multiple-file-upload-in-php) – mario Dec 05 '11 at 17:14

5 Answers5

4

If you loop through them as an array, it'll work just the same:

foreach($_FILES['file'] AS $key=>$file) {
    $filename = $file['tmp_name'];
    $size = $file['size'];
    $newfile = $_SERVER['DOCUMENT_ROOT'] . "/uploads/" . date("Ymd_his") . "_" . $filename;
    move_uploaded_file($filename, $newfile);
}

And it'll loop through each upload and process as such. Just be sure that you have something that changes between each (I added a timestamp) - otherwise you'll only end up with one file.

You were right with the inputs as

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

You can have as many of those as you'd like

Josh Toth
  • 754
  • 9
  • 23
  • I have question about move_uploaded_file($filename, $newfile); if I have newfile path abc/bcd/image.jpg and there is no folder bd will it create it? – Raimonds Dec 05 '11 at 17:40
  • No, you'll have to check with php using something like `is_dir($_SERVER['DOCUMENT_ROOT'] . "/abc/bcd")` and if it doesn't exist, use `mkdir($_SERVER['DOCUMENT_ROOT'] . "/abc/bcd")` – Josh Toth Dec 07 '11 at 21:02
1

Yes they'll work. $_FILES will be an array of uploaded files in PHP.

Tak
  • 11,428
  • 5
  • 29
  • 48
1

Yes this will of course work. Just have a look at the $_FILES superglobal array. All your uploaded files and their meta data will be stored there.

halfdan
  • 33,545
  • 8
  • 78
  • 87
0

As far as I am able to tell, if you want to do multiple file uploads from a html form, you need to have multiple file input boxes.

<input type='file' name='file1'>
<input type='file' name='file2'>
<input type='file' name='file3'>

Unless you have some type of java, javascript, flex, or similar multiple file upload framework to do the work for you.

Once you get it uploaded, the PHP script would look like:

<?
  foreach($_FILES as $file){
    move_uploaded_file($file[tmp_name],$target.$file[name]);
  }
?>
AndyD273
  • 7,177
  • 12
  • 54
  • 92
0

According to the W3C's HTML5 draft, you should add the multiple attribute :

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

Some browser (like Internet Explorer (even 9)) don't support multiple attribute but major other ones does.

Then, you can get all the file by looping into the $_FILES superglobal like that :

<?php

foreach ($_FILES['file']['error'] as $k => $error) {
    if (!$error) {
        move_uploaded_file($_FILES['file']['tmp_name'][$k], $your_dir.'/'.$_FILES['file']['name'][$k]);
    }
}
?>
V12L
  • 28
  • 6