Tuesday, November 13, 2018

How to Upload Multiple Files in PHP

How to Add Constants in Larvel 5

When we upload multiple files using PHP, the $_FILES variable as an array is not as a usual form as we think. It is formed by using file’s attribute as the key, not the index number. Thus, we can not loop through $_FILES variable with PHP 'foreach' loop statement.

1. $_FILES Variable Structure

The $_FILES variable is formed as follows:

array(5) {
["name"]=> array(2) { [0]=> string(17) "IMG_2586w_1x1.JPG"
                      [1]=> string(17) "IMG_2541w_1x1.JPG"
                    }
["type"]=> array(2) { [0]=> string(10) "image/jpeg"
                      [1]=> string(10) "image/jpeg"
                    }
["tmp_name"]=> array(2) { [0]=> string(26) "/private/var/tmp/phpwJBYnV"
                          [1]=> string(26) "/private/var/tmp/phpzbodce"
                        }
["error"]=> array(2) { [0]=> int(0)
                       [1]=> int(0)
                     }
["size"]=> array(2)  { [0]=> int(952096)
                       [1]=> int(879215)
                     }
}

This form of array needs to be converted to the form below, where the file’s index is the key instead of file’s attribute. This step is necessary for going to further process.

array(2) {
[0]=> array(5) { ["name"]=> string(17) "IMG_2586w_1x1.JPG"
                 ["type"]=> string(10) "image/jpeg"
                 ["tmp_name"]=> string(26) "/private/var/tmp/phpt3l7ct"
                 ["error"]=> int(0)
                 ["size"]=> int(952096)}
[1]=> array(5) { ["name"]=> string(17) "IMG_2541w_1x1.JPG"
                 ["type"]=> string(10) "image/jpeg"
                 ["tmp_name"]=> string(26) "/private/var/tmp/phpPIhTD2"
                 ["error"]=> int(0)
                 ["size"]=> int(879215)}
}

2. Converting Function

A converting function is written as follows:

/* convert multiple uploading files array $_FILES 
   to an array with an index as the key.
   from : [attribute] => [ [index] => [value], [index] => [value] ]
   to:    [index] => [ [attribute] => [value], [attribute] => [value] ]
*/
function convert_upload_file_array($upload_files) {
  $converted = array();

  foreach ($upload_files as $attribute => $val_array) {
    foreach ($val_array as $index => $value) {
      $converted[$index][$attribute] = $value;
    }
  }
  return $converted;
}

3. Multiple Uploading Files Setup

Now, the multiple files uploading process can be started from a upload form page:

upload.html

<form action="product.php" method="post" enctype="multipart/form-data">
  <input type="file" class="form-control-file" name="prod_pic[]" id="prod_pic[]">
  <button type="submit" class="btn btn-primary">Save Prodcut Info</button>
</form>

Note that the 'name' property of the <input> tag should be added an array tag '[]'. In addition, enctype="multipart/form-data" should be added in the tag in order to enable the file uploading function. Thus, the $_FILES variable can be an array to carry multiple files information.

<form .... enctype="multipart/form-data">
     :
  <input ... name="prod_pic[]">
  <input ... name="prod_pic[]">
     :
</form>  

Then, the product.php converts $_FILE variable and save files to the destination path.

product.php

<?php
  if (isset($_FILES['prod_pic'])) {
    $pics = convert_upload_file_array($_FILES['prod_pic']);

    foreach ($pics as $key => $pic) {
      $target = "images/{$pic['name']}";
      move_uploaded_file($pic['tmp_name'], $target);
    }
  }
?>

No comments:

Post a Comment