0

I need some help.I need to upload multiple file into folder using PHP. Here I can upload only single file. I am explaining my code below.

 <input style="padding:0;" type="file" class="filestyle form-control" data-size="lg" name="bannerimage" id="bannerimage">
<input style="padding:0;" type="file" class="filestyle form-control" data-size="lg" name="bannerimage1" id="bannerimage1">

Here is my PHP code.

<?php
 $imageName='bannerimage';
 $imagePath="uploads/";
 if ($_FILES['bannerimage']['name'] != ""){
    uploadImage($_FILES,$imageName,$imagePath,function($image){
       $newCustomerobj->image =$image['img'];
    }
 }
  public function uploadImage($files, $imageFieldName, $imageDirPath, $callback) {
        $result = array();
        //  print_r( $_FILES);exit;
        $imageName = generateRandomNumber() . '_' . $_FILES[$imageFieldName]['name'];
        // echo($_FILES[$imageFieldName]['tmp_name']);exit;
        $target_dir = $imageDirPath;
        $target_file = $target_dir . basename($imageName);
        $uploadOk = 1;
        $imageFileType = pathinfo($target_file, PATHINFO_EXTENSION);
        if (file_exists($target_file)) {
            $result['msg'] = "Sorry, file already exists.";
            $result['num'] = 0;
            $callback($result);
            $uploadOk = 0;
        }
        if ($_FILES[$imageFieldName]["size"] > 500000) {
            //  echo 'fileSize';exit;
            $result['msg'] = "Sorry, file size is large.";
            $result['num'] = 0;
            $callback($result);
            $uploadOk = 0;
        }
        if (($imageFileType != "jpg" && $imageFileType != "JPG") && $imageFileType != "png" && $imageFileType != "jpeg" && $imageFileType != "gif") {
            $result['msg'] = "Sorry, only .jpg,.jpeg,.gif and .png files are allowed.";
            $result['num'] = 0;
            $callback($result);
            $uploadOk = 0;
        }
        if ($uploadOk == 0) {
            $result['msg'] = "Sorry, Your file could not uploaded.";
            $result['num'] = 0;
            $callback($result);
        } else {
            if (move_uploaded_file($_FILES[$imageFieldName]['tmp_name'], $target_file)) {
                $result['msg'] = "Image has uploaded successfully.";
                $result['num'] = 1;
                $result['img'] = $imageName;
                $callback($result);
            } else {
                $result['msg'] = "Sorry, Your Image could not uploaded to the directory.";
                $result['num'] = 0;
                $callback($result);
            }
        }
    }
?>

Here I can only add one image but here I need to upload multiple image. Please help me to resolve this problem.

  • http://stackoverflow.com/a/43754965/4248328 OR http://stackoverflow.com/a/2233840/4248328 OR full example:- http://techstream.org/Web-Development/PHP/Multiple-File-Upload-with-PHP-and-MySQL – Alive to die - Anant May 17 '17 at 12:06
  • Please refer http://php.net/manual/en/features.file-upload.multiple.php and after this in php side try printing $_FILES. You will get your answer – Naincy May 17 '17 at 12:07
  • in your html form, change your name to `files[]` – Rotimi May 17 '17 at 12:08

1 Answers1

1

Use multiple attr

The multiple attribute is a boolean attribute.

When present, it specifies that multiple options can be selected at once.

HTML input

<input style="padding:0;" type="file" class="filestyle form-control" data-size="lg" multiple name="bannerimage[]" id="bannerimage">

Your PHP

 // set all images into array
function reArrayFiles(&$file_post)
{

    $file_ary = array();
    $file_count = count($file_post['name']);
    $file_keys = array_keys($file_post);

    for ($i = 0; $i < $file_count; $i++) {
        foreach ($file_keys as $key) {
            $file_ary[$i][$key] = $file_post[$key][$i];
        }
    }

    return $file_ary;
}


if ($_FILES['bannerimage']['name'] != "") {
    $file_ary = reArrayFiles($_FILES['bannerimage']);
    foreach ($file_ary as $file) {
        uploadImage($file, $imageName, $imagePath, function ($image) {
            $newCustomerobj->image = $image['img'];
        }
    }
}

UPDATE

uploading two separate images

<input style="padding:0;" type="file" class="filestyle form-control" data-size="lg" name="bannerimage" id="bannerimage">
<input style="padding:0;" type="file" class="filestyle form-control" data-size="lg" name="bannerimage1" id="bannerimage1">

PHP

$imageName = 'bannerimage';
$imageName2 = 'bannerimage1'; // for bannerimage1
$imagePath = "uploads/";

// uploading bannerimage
if ($_FILES['bannerimage']['name'] != "") {
    uploadImage($_FILES, $imageName, $imagePath, function ($image) {
        $newCustomerobj->image = $image['img'];
    }
 }

 // uploading bannerimage1
if ($_FILES['bannerimage1']['name'] != "") {
    uploadImage($_FILES, $imageName2, $imagePath, function ($image) {
        $newCustomerobj->image = $image['img'];
    }
 }
Community
  • 1
  • 1
Junius L
  • 15,881
  • 6
  • 52
  • 96
  • I have multiple field in my view.if it will be `bannerimage[]` like this ,then how to identify which image is coming from which file field. –  May 17 '17 at 12:26
  • `[]` means you are getting multiple images and you need to loop over the array and upload each image. – Junius L May 17 '17 at 12:28
  • Yes,But you used `multiple` attribute here i need to know also which image is belongs to which field if I am displaying it later. –  May 17 '17 at 12:35
  • If you want it that way just call `uploadImage()` twice. – Junius L May 17 '17 at 12:40
  • Can I get like `$_FILES['bannerimage']['name'] and $_FILES['bannerimage1']['name']` this in php file. –  May 17 '17 at 12:41
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/144463/discussion-between-julekgwa-and-subhra). – Junius L May 17 '17 at 12:47
  • Let me to test it and i will inform you. –  May 17 '17 at 12:48