7

I have the following code which works and uploads but it will not cycle through the array to upload every file, just the first file.

<form method="post" enctype="multipart/form-data" action="http://<?php echo $pageURL;?>">   
<input class="new" multiple="multiple" name="documents[]" type="file" />
<input class="new" multiple="multiple" name="documents[]" type="file" />
<input type="submit" class="button" name="addMaterials" value="Add" />

<?php

    foreach($_FILES['documents']['tmp_name'] as $key => $tmp_name)
        {
            $file_name = $key.$_FILES['documents']['name'][$key];
            $file_size =$_FILES['documents']['size'][$key];
            $file_tmp =$_FILES['documents']['tmp_name'][$key];
            $file_type=$_FILES['documents']['type'][$key];  
            move_uploaded_file($file_tmp,"files/".time().$file_name);
        }
?>

I need it to cycle through my documents[] file array.

Example print_r() of the documents array:

Array ( 
    [name] => Array ( [0] => AcroRd32.exe ) 
    [type] => Array ( [0] => application/x-msdownload ) 
    [tmp_name] => Array ( [0] => C:\xampp\tmp\phpE8BD.tmp ) 
    [error] => Array ( [0] => 0 ) 
    [size] => Array ( [0] => 1343112 ) 
    )

Any help appreciated.

3
  • Can you show us the HTML for your file upload fields? Commented Apr 11, 2013 at 5:52
  • Which array would be this by the way? $key.$_FILES['... Commented Apr 11, 2013 at 5:53
  • html added, all I need it to do is upload every file. Commented Apr 11, 2013 at 5:54

6 Answers 6

19

you can use my updated code and as per my demo it is working perfect for multiple file upload

 <?php
if(isset($_FILES['documents'])){

foreach($_FILES['documents']['tmp_name'] as $key => $tmp_name)
{
    $file_name = $key.$_FILES['documents']['name'][$key];
    $file_size =$_FILES['documents']['size'][$key];
    $file_tmp =$_FILES['documents']['tmp_name'][$key];
    $file_type=$_FILES['documents']['type'][$key];  
    move_uploaded_file($file_tmp,"galleries/".time().$file_name);
}
}else{
echo "<form enctype='multipart/form-data' action='test1.php' method='POST'>";
 echo "File:<input name='documents[]' multiple='multiple' type='file'/><input type='submit' value='Upload'/>";

 echo "</form>";
}
?>
Sign up to request clarification or add additional context in comments.

2 Comments

This still only loops once!
if files value is array of array newfiles[][0],newfiles[][1] ?
3

For anyone trying to do it with a single file php function (i`m using classes, but you can change to a function):

html:

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

php:

if (isset($_FILES['foto'])) {

      $arquivo = array();
    foreach ($_FILES['foto']["name"] as $file=>$key) {

                    // the empty input files create an array index too, so we need to
                    // check if the name exits. It means the file exists.
        if (!empty($_FILES['foto']["name"][$file])) {
          $arquivo ["name"] = $_FILES['foto']["name"][$file];
          $arquivo ["type"] = $_FILES['foto']["type"][$file];
          $arquivo ["tmp_name"] = $_FILES['foto']["tmp_name"][$file];
          $arquivo ["error"] = $_FILES['foto']["error"][$file];
          $arquivo ["size"] = $_FILES['foto']["size"][$file];

$foto = new foto(); // create an obj foto
    // $arquivo means file, it`s our file format as a single $_file['file']
if ($foto -> upload($arquivo)) { // if its uploaded than save
    $foto -> save();
}


    }

    } 

}

on my foto class:

public function upload($foto) {

    $upload_dir = "D:/xampp/htdocs/prova/fotos/";
    $file_dir = $upload_dir . $foto["name"];

    $move = move_uploaded_file($foto["tmp_name"], $file_dir);
    $this -> arquivo = $foto["name"]; // use this to save to db later

    // this serves to return true if the file is uploaded
    $retorno = ($move) ? 1 : 0; 
    return $retorno;

}

Comments

2

Try with this code for multifile upload

<form method="post" action="upload-page.php" enctype="multipart/form-data">
 <input name="filesToUpload[]" id="filesToUpload" type="file" multiple="" />
</form>

In PHP

if(count($_FILES['uploads']['filesToUpload'])) {
foreach ($_FILES['uploads']['filesToUpload'] as $file) {

    //do your upload stuff here
    echo $file;

}
}

To show the file name using javascript

//get the input and UL list
var input = document.getElementById('filesToUpload');
var list = document.getElementById('fileList');

//empty list for now...
while (list.hasChildNodes()) {
list.removeChild(ul.firstChild);
}

//for every file...
for (var x = 0; x < input.files.length; x++) {
//add to list
var li = document.createElement('li');
li.innerHTML = 'File ' + (x + 1) + ':  ' + input.files[x].name;
list.append(li);
}

Comments

1

it's easy to upload multiple files, follow these steps.

  1. use array notation means square brackets with the name of the input like this
  2. <input type="file" id="indtbl_logo[]" name="indtbl_logo[]" multiple />
  3. you can loop throught this using $_FILES Global Variable
    foreach ($_FILES['indtbl_logo']['tmp_name'] as $key => $tmp_name) {
        $file_name = $key . $_FILES['indtbl_logo']['name'][$key];
        $file_size = $_FILES['indtbl_logo']['size'][$key];
        $file_tmp = $_FILES['indtbl_logo']['tmp_name'][$key];
        $file_type = $_FILES['indtbl_logo']['type'][$key]; 

        echo $file_name;
        echo "<br>"; 
    }

Comments

0

Try this way of loop your documents array()

<?php 

foreach($_FILES['documents']['tmp_name'] as $key => $tmpName) {

  $file_name = $_FILES['documents']['name'][$key];
  $file_type = $_FILES['documents']['type'][$key];
  $file_size = $_FILES['documents']['size'][$key];
  $file_tmp  = $_FILES['documents']['tmp_name'][$key];

  move_uploaded_file($file_tmp,"files/".time().$file_name);
}

?>

1 Comment

It seems that the array only has one file within it anyway, so I'm not sure why my files are not added to the array before I execute this code.
0

File upload using multiple input fields.

HTML

<form action="" method="post" enctype="multipart/form-data">
          <p><input type="file" name="file_array[]"></p>
          <p><input type="file" name="file_array[]"></p>
          <p><input type="file" name="file_array[]"></p>
          <input type="submit" value="Upload all files">
        </form>

PHP

<?php
if(isset($_FILES['file_array'])){
    $name_array = $_FILES['file_array']['name'];
    $tmp_name_array = $_FILES['file_array']['tmp_name'];
    $type_array = $_FILES['file_array']['type'];
    $size_array = $_FILES['file_array']['size'];
    $error_array = $_FILES['file_array']['error'];
    for($i = 0; $i < count($tmp_name_array); $i++){
        if(move_uploaded_file($tmp_name_array[$i], "test_uploads/".$name_array[$i])){
            echo $name_array[$i]." upload is complete<br>";
        } else {
            echo "move_uploaded_file function failed for ".$name_array[$i]."<br>";
        }
    }
}
?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.