Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • CakePHP Image Upload Using Component

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 177
    Comment on it

    Hello Readers, In this blog tutorial we will explain how to upload Image in CakePHP using component.

    Create a file called UploadComponent.php (path:app/controller/component) & paste the following code.

         <?php    
        class UploadComponent extends Component {
            public function start_upload($file_Details) {
             print_r($file_Details);
                $filename = ($file_Details['name']);
                $filename = time().$filename;
                $file_path =   WWW_ROOT  . 'My/'.$filename;
                move_uploaded_file($file_Details['tmp_name'],$file_path);
                return $filename;
             }
              }
        ?>
    
    


    WWW_ROOT. 'My'; //set webroot path with the folder name (My) where the file is to be uploaded.

    time().$filename; //Append the random no before the file name.

     

    Create a file UsersController.php (path:app/controller/) & paste the below function code in your UsersController class

        public function uploadfile() {
          $file_name1 = '';
          if ($this->request->is('post')) {
          $file_name1=$this->Upload->start_upload($this->data['uploadFile']['pdf_path']);   
                $data=array();
                $data['User']['id'] = CakeSession::read("Auth.User.id");
                $data['User']['imagename'] = $file_name1;
                $this->User->save($data);       // save the file path in database
                echo $data['User']['id']." + ".$data['User']['imagename'];
                 }
          $this->set('image',$file_name1);
        }

    Create a file called uploadfile.ctp (Path: app/view/users ) & paste the following code.
      

      <div class="users view">
        <h2><?php echo('User'); ?></h2>
        <?php
        echo $this->Form->create('uploadFile', array('type' => 'file'));
        echo $this->Form->input('pdf_path', array('type' => 'file','label' => 'Pdf'));
        echo $this->Form->end('Upload file');
        $image_src = $this->webroot.'img/'.$image;
        ?>
        <img src="<?php echo $image_src;?>">
        </div>
    
    

     

 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: