CakePHP | File Upload

 In your view

 <div class="col-md-4">
  <div class="box box-warning">
   <div class="box-header">
   <h3 class="box-title"><?php echo __('Profile Picture'); ?></h3>
   </div>
   <div class="box-body">
   <?php echo $this->Form->create('User', array('action' => 'upload_profile_pic', 'role' => 'form', 'enctype'=>'multipart/form-data')); ?>
    <fieldset>
     <div class="row">
      <div class="col-md-12">
       <div class="form-group">
        <?php echo $this->Form->input('photo', array('label' => false, 'type' => 'file', 'class' => 'form-control', 'id' => 'photo')); ?>
       </div><!-- .form-group -->
      </div>
     </div>
     <div class="row">
      <br>
      <div class="col-md-6 col-md-offset-3">
       <?php echo $this->Form->submit('Submit', array('class' => 'btn btn-lg btn-block btn-warning')); ?>
      </div>
     </div>
    </fieldset>
   <?php echo $this->Form->end(); ?>
   </div>
  </div>
 </div>

In your UsersController.php
/**
 * add method
 *
 * @return void
 */
 public function upload_profile_pic() {
  if ($this->request->is('post')) {
   ini_set('upload_max_filesize', '64M'); //optional
   set_time_limit(600); //optional
   $filename = null;
   $newFileName = null;
    if (!empty($this->request->data['User']['photo']['tmp_name']) && is_uploaded_file($this->request->data['User']['photo']['tmp_name'])) {
        // Strip path information
        $image_dimension = getimagesize($this->request->data['User']['photo']['tmp_name']);
        $size = $this->request->data['User']['photo']['size'];
        //get filename
     $filename = Inflector::slug(pathinfo($this->request->data['User']['photo']['name'], PATHINFO_FILENAME)).'.'.pathinfo($this->request->data['User']['photo']['name'], PATHINFO_EXTENSION);
     
     $letters = 'abcefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890';
        $randomName = substr(str_shuffle($letters), 0, 10);
        //create new filename to avoid replacing same filename
     $newFileName = date('YmdHis').'_'.$randomName.'.'.pathinfo($this->request->data['User']['photo']['name'], PATHINFO_EXTENSION); 
        //upload file (file to upload, path to upload)
        move_uploaded_file($this->request->data['User']['photo']['tmp_name'], App::themePath('CakeAdminLTE').'webroot/img/profile_pic/'. $newFileName);
    
    
     $image_width = $image_dimension[0];
     $image_height = $image_dimension[1];

     $this->request->data['User']['user_id'] = AuthComponent::User('id');
     // Set the file-name only to save in the database
     $this->request->data['User']['file_name'] = $filename;
     $this->request->data['User']['new_file_name'] = $newFileName;
     $this->request->data['User']['size'] = $size;
     $this->request->data['User']['dimension'] = $image_width.' x '.$image_height;


     if ($this->User->save($this->request->data)) {
      $this->Session->setFlash(__('The profile picture has been saved'), 'flash/success');
      $this->redirect(array('action' => 'profile', AuthComponent::User('id')));
     }
   } else {
    $this->Session->setFlash(__('The profile pic could not be saved. Please, try again.'), 'flash/error');
    $this->redirect(array('action' => 'update', AuthComponent::User('id')));
   }
  }
  
 } 

Comments