Skip to main content

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 class="box-body">
   <?php echo $this->Form->create('User', array('action' => 'upload_profile_pic', 'role' => 'form', 'enctype'=>'multipart/form-data')); ?>
     <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 class="row">
      <div class="col-md-6 col-md-offset-3">
       <?php echo $this->Form->submit('Submit', array('class' => 'btn btn-lg btn-block btn-warning')); ?>
   <?php echo $this->Form->end(); ?>

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')));
Post a Comment

Popular posts from this blog

CakeAdminLTE is based on AdminLTE Theme for CakePHP 2.4+

CakeAdminLTE v.1.0 CakeAdminLTE is based on Twitter Bootstrap 3.0+ and AdminLTE Theme for CakePHP 2.4+

AdminLTE Dashboard and Control Panel Template  Responsive admin template Free & open source Built with Bootstrap 3 Easy to customize Quick StartDownload the .zip fileExtract the files into your CakePHP folderTo enable your theme add the following to your "app/Controller/AppController.php" class:public$theme="CakeAdminLTE"; IMPORTANT: If you would like to bake your app, please make sure you have enabled the theme before baking. DOWNLOAD: CakeAdminLTE

CakePHP: COUNT data and GROUP BY date

Goal: Count Tip Offs created per day for a month to use for graph
Problem: created field name is in datetime format: Y-m-d H:i:s
Solution: format SQL Query date: DATE_FORMAT(TipOff.created, '%Y-%m-%d')

Inside the function of controller
<?php$tipOffsMonthly=$this->TipOff->find('all', array('conditions'=>array('AND'=>array('TipOff.electric_cooperatives_id'=>AuthComponent::User('electric_cooperatives_id'), 'TipOff.created BETWEEN ? AND ?'=>array($first_day, $last_day))), 'fields'=>array("COUNT('created') as per_day", 'created'), 'group'=>array("DATE_FORMAT(TipOff.created, '%Y-%m-%d')") ));?>

CakePHP: Calling function from other controller

Import Controller of the function you want to use.

Once you imported  the controller you can call any function of this controller.

<?php//Import controller  App::import('Controller','SmsOutgoings');class ReportsController extends AppController {publicfunctionadd(){$message="Notification: New report submitted!";//Instantiation$SmsOutgoings=new SmsOutgoingsController;//Call a method from SmsOutgoingsControllerwith parameter$SmsOutgoings->notify_user($user_id,$message);}}?> Other way is by using the requestAction() function
<?phpclass ReportsController extends AppController {publicfunctionadd(){$message="Notification: New report submitted!";//Call a method from SmsOutgoingsController with parameter$this->requestAction('/sms_outgoings/notify_user',array($user_id,$message))