06.05
php
saved
adrien.gibrat
Note
An upload behavior that give standard methods to validate uploaded files and to get mimetype & filesize in model.
An upload behavior that give standard methods to validate uploaded files and to get mimetype & filesize in model.
- <?php
- /** Example how to use upload behavior in your model:
- var $actsAs = array(
- 'Upload' => 'audio_file', // use one field with default options
- // 'Upload' => array('audio_file', 'image_file') // use two fields with default options
- // 'Upload' => array('audio_file' => array( // use one fields with custom options
- // 'dir'=> '/data', // absolute path where files will be uploaded
- // 'filename'=> 'my_rename_method', // true will use the uploaded file name, false will generate a unique filename, 'my_rename_method' will call the user defined model method my_rename_method to generate a filename
- // 'overwrite' => false, // do we overwrite existing files?
- // 'delete' => true, // do we delete file on delete?
- // 'cascade' => true, // do we delete all the entries in model that use the same file on delete?
- // 'messages' => array( // error messages
- // UPLOAD_ERR_OK => 'Do you try to hack?',
- // UPLOAD_ERR_INI_SIZE => 'Server allow files under %s.',
- // UPLOAD_ERR_FORM_SIZE => 'This form allow files under %s.',
- // UPLOAD_ERR_PARTIAL => 'Partial file upload!',
- // UPLOAD_ERR_NO_FILE => 'Please provide a file.',
- // UPLOAD_ERR_NO_TMP_DIR => 'No temporary folder!',
- // UPLOAD_ERR_CANT_WRITE => 'Chmod the temporary folder!',
- // UPLOAD_ERR_EXTENSION => 'File upload stopped!',
- // 'no_dir' => 'No upload directory!',
- // 'no_write' => 'Chmod the upload directory!',
- // 'file_exists' => 'The file already exists!',
- // 'no_move' => 'Sorry, something went wrong!.'
- // )
- // )
- );
- var $validate = array(
- 'audio_file' => array(
- 'upload_type' => array(
- 'rule' => array('upload_type', array('audio/x-wav','audio/mpeg')),
- 'allowNoFile' => true, // allowNoFile set to true will not trigger error if no file uploaded
- 'message' => 'Please provide a wav or mp3 file.'
- ),
- 'upload_size' => array(
- 'rule' => array('upload_size', '30k', null),
- 'allowNoFile' => true, // allowNoFile is like allowEmpty for file upload
- 'message' => 'Please provide file up to 30 ko.'
- ),
- 'uploaded' => array(
- 'rule' => 'uploaded',
- 'allowNoFile' => true, // return contextual error message only if upload process fails
- 'message' => 'Error while uploading file.' // so default message is not used if allowNoFile is true
- ),
- 'create' => array(
- 'rule' => 'uploaded',
- 'allowNoFile' => false,
- 'on' => 'create', // you can use on create/update rules!
- 'message' => 'Please provide a file.'
- )
- ),
- // Or you can validate the model data set with beforeValidate (replace the file's upload_size and upload_type rules)
- // display of errors in view will be affected, use $form->error('filesize') and $form->error('mimetype').
- // 'mimetype' => array(
- // 'rule' => array('in', array('audio/x-wav','audio/mpeg')),
- // 'message' => 'Please provide a wav or mp3 file.'
- // ),
- // 'filesize' => array(
- // 'rule' => array('comparison', '>', 30720),
- // 'message' => 'Please provide file up to 30 ko.'
- // ),
- );
- function in($check, $allowed) {
- $data = array_shift(array_values($check));
- return in_array($data, (array)$allowed);
- }
- function my_rename_method($filename) {
- return md5(uniqid(time())) . '-' . $filename;
- }
- // function beforeValidate() {
- // $this->data['mimetype'] = $this->get_mimetype('audio_file');
- // $this->data['filesize'] = $this->get_filesize('audio_file');
- // return true;
- // }
- */
- }
- class UploadBehavior extends ModelBehavior {
- 'dir' => null,
- 'filename' => null,
- 'overwrite' => null,
- 'delete' => null,
- 'cascade' => null,
- UPLOAD_ERR_OK => 'Do you try to hack the form?',
- UPLOAD_ERR_INI_SIZE => 'The file is too large, max filesize on server is %s.',
- UPLOAD_ERR_FORM_SIZE => 'The file is too large, max filesize on form is %s.',
- UPLOAD_ERR_PARTIAL => 'The file was only partially uploaded.',
- UPLOAD_ERR_NO_FILE => 'Please provide a file.',
- UPLOAD_ERR_NO_TMP_DIR => 'The servers temporary folder is missing.',
- UPLOAD_ERR_CANT_WRITE => 'Failed to write file to disk.',
- UPLOAD_ERR_EXTENSION => 'File upload stopped by extension.',
- 'no_dir' => 'The servers upload directory is missing.',
- 'no_write' => 'Failed to write to the upload directory.',
- 'file_exists' => 'The file %s already exists.',
- 'no_move' => 'Failed to move file the upload directory.'
- )
- );
- $field = $options;
- } elseif(!$model->hasField($field)) {
- continue;
- }
- );
- $settings[$field]['dir'] = FILES . low(Inflector::pluralize($model->alias) . DS . $field) . DS;
- $settings[$field]['dir'] .= DS;
- }
- }
- $this->settings[$model->alias] = $settings;
- }
- /* Behavior save callback processing uploaded file */
- function beforeSave(&$model) {
- foreach($this->settings[$model->alias] as $field => $options) {
- return $model->invalidate($field, $messages['no_dir']) && false;
- }
- return $model->invalidate($field, $messages['no_write']) && false;
- }
- if($filename === true) {
- $filename = $model->data[$model->alias][$field]['name'];
- } else {
- }
- $file = $dir . $filename;
- }
- return $model->invalidate($field, $messages['no_move']) && false;
- }
- $model->data[$model->alias][$field] = $filename;
- }
- }
- }
- /* Behavior delete callback to delete file */
- function beforeDelete(&$model, $cascade) {
- $model->read(null, $model->id);
- foreach($this->settings[$model->alias] as $field => $options) {
- $file = $options['dir'] . $model->data[$model->alias][$field];
- return false;
- }
- }
- }
- return true;
- }
- /* Behavior delete callback for cascade deletion */
- function afterDelete(&$model) {
- foreach($this->settings[$model->alias] as $field => $options) {
- if(!empty($options['cascade']) && $used = $model->findAll("{$model->alias}.$field = '{$model->data[$model->alias][$field]}'")){
- foreach($used as $line) {
- $model->del($line[$model->alias]['id']);
- }
- }
- }
- }
- /* Behavior model method to get uploaded file mimetype */
- function get_upload_mimetype($model, $field) {
- return mime_content_type($model->data[$model->alias][$field]['tmp_name']);
- }
- }
- /* Behavior model method to get uploaded file size */
- function get_upload_filesize($model, $field) {
- }
- }
- /* Behavior validation method to check uploaded file error */
- function uploaded($model, $check, $param) {
- $messages = $this->settings[$model->alias][$field]['messages'];
- $message = @$messages[$data['error']];
- switch($data['error']) {
- case UPLOAD_ERR_OK:
- if($this->upload_check($model, $check, $param)) {
- return true;
- }
- break;
- case UPLOAD_ERR_NO_FILE:
- case UPLOAD_ERR_INI_SIZE:
- break;
- case UPLOAD_ERR_FORM_SIZE:
- break;
- }
- $model->invalidate($field, $message);
- return true; // return true to show the custom message
- }
- /* Behavior validation method to check if file is uploaded via HTTP POST */
- function upload_check($model, $check, $param) {
- }
- /* Behavior validation method to check uploaded file name */
- function upload_name($model, $check, $regex = VALID_NOT_EMPTY, $param) {
- }
- /* Behavior validation method to check uploaded file type */
- function upload_type($model, $check, $allowed = array('image/jpeg', 'image/png', 'image/gif'), $param) {
- }
- /* Behavior validation method to check uploaded file size */
- function upload_size($model, $check, $min = 0, $max = null, $param) {
- }
- /* Private size formater helper */
- function _size_format($size) {
- $size
- );
- }
- /* Private formated size to integer helper */
- function _size_int($size) {
- case 'm':
- $size *= 1024;
- case 'k':
- $size *= 1024;
- }
- }
- }
- ?>
Parsed in 0.454 seconds, using GeSHi 1.0.7.14