Last active
August 29, 2015 14:01
-
-
Save julienhay/fb2c712e6b8a3a819f4f to your computer and use it in GitHub Desktop.
Upload files example Cakephp + CRUD
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
App::uses('AppController', 'Controller'); | |
App::uses('Folder', 'Utility'); | |
App::uses('File', 'Utility'); | |
App::uses('CakeTime', 'Utility'); | |
class ExampleController extends AppController { | |
public static $accept_types = array( | |
'pdf' => 'application/pdf', | |
'doc' => 'application/msword', | |
'xls' => 'application/vnd.ms-excel', | |
'ppt' => 'application/vnd.ms-powerpoint', | |
'docx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', | |
'xlsx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', | |
'pptx' => 'application/vnd.openxmlformats-officedocument.presentationml.presentation', | |
'ppsx' => 'application/vnd.openxmlformats-officedocument.presentationml.slideshow', | |
); | |
public function implementedEvents() | |
{ | |
return parent::implementedEvents() + array( | |
'Crud.beforeSave' => '_beforeSave', | |
); | |
} | |
public function _beforeSave(CakeEvent $event) | |
{ | |
$root_folder = WWW_ROOT.'files'.DS.'uploads/TONFOLDER'; | |
$folder_site = new Folder($root_folder, true, 0755); | |
if ( !empty($this->request->data[$this->modelClass]['CHAMPS_FICHIER']['tmp_name']) && is_uploaded_file($this->request->data[$this->modelClass]['CHAMPS_FICHIER']['tmp_name']) ) | |
{ | |
$file_tmp = new File($this->request->data[$this->modelClass]['CHAMPS_FICHIER']['tmp_name']); | |
$type_mime = $file_tmp->mime(); | |
if(in_array($file_tmp->mime(), ExampleController::$accept_types)) | |
{ | |
$file_origin = new File($this->request->data[$this->modelClass]['CHAMPS_FICHIER']['name']); | |
$filename = Inflector::slug($file_origin->name()).'.'.$file_origin->ext(); | |
$file_real = new File($folder_site->path.DS.$filename); | |
if($file_real->exists()) | |
{ | |
$filename = Inflector::slug(md5( time().$file_origin->name() )).'.'.$file_origin->ext(); | |
} | |
move_uploaded_file( | |
$this->data[$this->modelClass]['CHAMPS_FICHIER']['tmp_name'], | |
$folder_site->path.DS.$filename | |
); | |
$this->request->data[$this->modelClass]['CHAMPS_FICHIER'] = $filename; | |
$this->request->data[$this->modelClass]['mime'] = $type_mime; | |
$this->request->data[$this->modelClass]['extfile'] = array_search($type_mime, ExampleController::$accept_types); | |
return true; | |
} | |
else | |
{ | |
unset($this->request->data[$this->modelClass]['CHAMPS_FICHIER']); | |
$this->Session->setFlash(__('Only file type Power Point, Word or PDF please'),'default',array('class' => 'error')); | |
if(isset($this->request->data[$this->modelClass]['id'])) | |
{ | |
$this->redirect(array( | |
'action' => 'edit', | |
$this->request->data[$this->modelClass]['id'], | |
)); | |
} | |
else | |
{ | |
$this->Session->write('data', $this->request->data); | |
$this->redirect(array( | |
'action' => 'add', | |
)); | |
} | |
} | |
} | |
else | |
{ | |
unset($this->request->data[$this->modelClass]['CHAMPS_FICHIER']); | |
return false; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment