06.05
php
saved
adrien.gibrat
Note
An upload behavior that give standard methods to validate uploaded files and set mimetype & filesize automagic model fields.
Do just upload, but do it well.
Sorry but it's not well documented!
An upload behavior that give standard methods to validate uploaded files and set mimetype & filesize automagic model fields.
Do just upload, but do it well.
Sorry but it's not well documented!
- <?php
- /**
- Exemple how to use this behavior in your model:
- var $actsAs = array(
- 'Upload' => 'audio_file',// use one field with default options
- // 'Upload' => array('audio_file','image_file')// use 2 fields with default options
- // 'Upload' => array('audio_file'=>array('dir'=>'/data'),'image_file'=>array('filename'=>'my_method'))// use 2 fields with custom options
- );
- var $validate = array(
- 'audio_file' => array(
- 'upload_type' => array(
- 'rule' => array('upload_type', array('audio/x-wav','audio/mpeg')),
- 'no_upload' => true,// no_upload is what should return the test is no uploaded file
- 'message' => 'Please provide a wav or mp3 file.'
- ),
- 'upload_size' => array(
- 'rule' => array('upload_size', '30k', null),
- 'no_upload' => true,
- 'message' => 'Please provide file up to 30 ko.'
- ),
- 'uploaded' => array(
- 'rule' => array('uploaded', null),
- 'no_upload' => true,
- 'message' => 'Error while uploading file.'
- ),
- 'create' => array(
- 'rule' => array('uploaded', null),
- 'on' => 'create',
- 'message' => 'Please provide a file.'
- )
- ),
- );
- */
- }
- class UploadBehavior extends ModelBehavior {
- 'dir' => null,
- 'filename' => null,
- 'overwrite' => null,
- 'cascade' => null,
- '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.'
- )
- );
- }
- foreach($config as $field => $options) {
- $field = $options;
- } elseif(!$model->hasField($field)) {
- continue;
- }
- $options['dir'] = FILES . low(Inflector::pluralize($model->name) . DS . $field) . DS;
- $options['dir'] .= DS;
- }
- $config[$field] = $options;
- }
- $this->fields = $config;
- }
- function beforeSave(&$model) {
- foreach($this->fields as $field => $options) {
- if (!empty($model->data[$model->name][$field]['tmp_name']) && file_exists($model->data[$model->name][$field]['tmp_name'])) {
- return $model->invalidate($field, $options['messages']['no_dir']) && false;
- }
- return $model->invalidate($field, $options['messages']['no_write']) && false;
- }
- if($options['filename'] === true) {
- $filename = $model->data[$model->name][$field]['name'];
- $filename = call_user_func(array($model, $options['filename']), $model->data[$model->name][$field]['name']);
- } else {
- }
- $file = $options['dir'] . $filename;
- }
- return $model->invalidate($field, $options['messages']['no_move']) && false;
- }
- $model->data[$model->name][$field] = $filename;
- $model->data[$model->name]['mimetype'] = mime_content_type($file);
- } else {
- //Not an uploaded file
- }
- }
- }
- }
- function beforeDelete(&$model, $cascade) {
- $model->read(null, $model->id);
- foreach($this->fields as $field => $options) {
- $file = $options['dir'] . $model->data[$model->name][$field];
- return false;
- }
- }
- }
- }
- return true;
- }
- function afterDelete(&$model) {
- foreach($this->fields as $field => $options) {
- $used = $model->findAll("{$model->name}.$field = '{$model->data[$model->name][$field]}'");
- foreach($used as $line) {
- $model->del($line[$model->name]['id']);
- }
- }
- }
- }
- }
- /* check uploaded file error */
- if(!$this->upload_check($model, $check, $param)) {
- return false;
- }
- 'Error while uploading file.',
- UPLOAD_ERR_INI_SIZE => 'The file is too large, max filesize on server: %s.',
- UPLOAD_ERR_FORM_SIZE => 'The file is too large, max filesize on form: %s.',
- UPLOAD_ERR_PARTIAL => 'The file was only partially uploaded.',
- 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.'
- );
- $messages[$data['error']] :
- switch($data['error']) {
- case UPLOAD_ERR_OK:
- return true;
- case UPLOAD_ERR_NO_FILE:
- case UPLOAD_ERR_INI_SIZE:
- break;
- case UPLOAD_ERR_FORM_SIZE:
- break;
- }
- // return true to show the custom message
- }
- /* check if file is uploaded via HTTP POST */
- function upload_check($model, $check, $param) {
- }
- /* check uploaded file name */
- function upload_name($model, $check, $regex = VALID_NOT_EMPTY, $param) {
- }
- /* check uploaded file type */
- function upload_type($model, $check, $allowed = array('image/jpeg', 'image/png', 'image/gif'), $param) {
- }
- /* check uploaded file size */
- function upload_size($model, $check, $min = 0, $max = null, $param) {
- }
- function _size_format($size) {
- $size
- );
- }
- function _size_int($size) {
- case 'm':
- $size *= 1024;
- case 'k':
- $size *= 1024;
- }
- }
- }
- ?>
Parsed in 0.493 seconds, using GeSHi 1.0.7.14