Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Multiple Image Uploads in Cakephp

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 2
    • 0
    • 1.48k
    Comment on it

     

    Hello Readers.! Here is a small blog on how to upload multiple images in database using cakephp.File uploading, nowadays is a very basic and common functionality. Here in this blog we will be uploading an image which will be stored on the server and the url of that file will be stored in the database so we can easily access it when a user has logged in.

     

    Create table Image in database with fields id name and email.

    CREATE TABLE `images` (
     `id` int(11) NOT NULL PRIMARY KEY,
     `name` VARCHAR(45) ,
     `email` VARCHAR(255),
    );

     

     

    While creating a new form in cakephp with input type file, set enctype'=>'multipart/form-data' and code should be like this:

    <?php echo $this->Form->input('user_image.',array('type'=>'file','label' => false,'placeholder' => 'Upload images','id'=>"inputFile",'multiple','onchange'=>'readURL(this)'));?>

     

    Next, create function in Controller:

    foreach ($this->request->data['User']['user_image'] as $key => $photos) {
    					
    	$imgData = array();
    	$imageData = array();
    	$imgData['file'] = $photos;
    	$imgData['name'] = $photos['name'];
    	$imgData['path'] = WWW_ROOT . '' . 'images/uploads/';
    	$user_image = $this->Qimage->copy($imgData);
    	$imageData['Image']['name'] = $user_image;
    	$imageData['Image']['email'] = $this->request->data['User']['email'];
    	$this->Image->create();
    	$this->Image->save($imageData);
    	}
    }
    else{
    	$user_image = "";
    }

     

    Our 'User' array in $this->data will now contain all the details for the 2 files, we can check the values using the print_r function.

     

    Enjoy Coding :)

    Multiple Image Uploads in Cakephp

 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: