Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to upload multiple file upload in CakePHP 2.x?

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 2.06k
    Comment on it

    Hi Reader's,
    Welcome to FindNerd,today we are going to discuss how to upload multiple file upload in CakePHP 2.x?

    If we want to upload multiple images in our CakePHP web application with the help of input browse box. So for that we have to follow bellow process.

    We have to create view for uploading multiple images which will like below.

    HTML 5 Multiple file Tag:

    <input name = 'upload[]' type = 'file multiple'>

    Use CakePHP form helper to build browse button

    <?php
      echo $this->Form->create('User', array('type' => 'file'));
      echo $this->Form->input('files.', array('type' => 'file', 'multiple'));
      echo $this->Form->end('upload');
    ?>

    Add a dot(.) in the end of the name shows all the images selected.
    for example

    file.

     If we want to upload two file which name is image1.jpg and image2.png

     We have to use  $this->request->data  property in our controller function.

     Then our controller code will like below code:

    <?php
      public function upload(){
        if(!empty($this->request->data)){
           /*  Start code for o Uploading images*/
          foreach ($this->request->data as $result) {  
            $name = $result['name'];
              if( !empty($name)){     // Check For Empty Values 
                $tmprary_name = $result['tmp_name'];
                $temp = explode(".", $name);
                $newfilename = uniqid(). '.' . end($temp);
                  if (move_uploaded_file($tmprary_name , $target_dir.$newfilename)) {
                    echo "The file ". basename( $name). " has been successfully uploaded.<BR/>";
                  }else {
                       echo "Sorry, there was an error uploading your file.<br/>";
                  }
              }
          }
    
        }
      }
    ?>

    $this->request->data contains images data like this

    <?php
        Array
        (
          [Model] => Array
          (
            [files] => Array
            (
              [0] => Array
              (
                [name] => image1.jpg
                [type] => image/jpeg
                [tmp_name] => // your temp path
                [error] => 0
                [size] => 1826587
              )
                  [1] => Array
                  (
                    [name] => image2.jpg
                    [type] => image/jpeg
                    [tmp_name] => //your temp path
                    [error] => 0
                    [size] => 1127346
                  )
            )
        )
      )
     ?>
    

     I hope this blog will help to you upload multiple images in your CakePHP web application.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: