06.26 php saved
Linnk
Tags add more
file upload mimetype  
Note
A smart file upload behavior which uses mime-types to sort and validate files based on criteria set by the user. Use it in your model like this:


var $actsAs = array(
'File' => array(
'image' => array(
'/image\/(jpeg|gif|png)/' => array(
'destination' => 'files/images/',
'naming' => 'hash',
'max' => 1024 * 1024
)
),
'document' => array(
'/application\/pdf/' => array(
'destination' => 'files/pdfs/',
'naming' => 'hash',
'max' => 1024 * 1024 * 1024
)
)
)
);

function hash(&$data) {
return sha1_file($data['tmp_name']);
}
  1. <?php
  2. class FileBehavior extends ModelBehavior {
  3.    
  4.     var $settings = array();
  5.    
  6.     function setup(&$Model, $settings = array()) {
  7.         if (!isset($this->settings[$Model->alias])) {
  8.             $this->settings[$Model->alias] = array(
  9.                 '/.*/' => array(
  10.                     'destination' => 'files/',
  11.                     'max' => 0,
  12.                     'naming' => 'hash'
  13.                 )
  14.             );
  15.         }
  16.         $this->settings[$Model->alias] = array_merge(
  17.              $this->settings[$Model->alias],
  18.              ife(is_array($settings), $settings, array())
  19.         );
  20.     }
  21.    
  22.     function beforeSave(&$Model) {
  23.         foreach ($this->settings[$Model->alias] as $field => $types) {
  24.             foreach ($types as $pattern => $options) {
  25.                 if (preg_match($pattern, $Model->data[$Model->alias][$field]['type'])) {
  26.                     $supported = true;
  27.                     $name = $Model->{$options['naming']}(&$Model->data[$Model->alias][$field]);
  28.    
  29.                     if ($options['max'] > 0 && $Model->data[$Model->alias][$field]['size'] >= $options['max']) {
  30.                         $Model->invalidate($field, __('Sorry, this file is too big.', true));
  31.                         return false;
  32.                     }
  33.                    
  34.                     $path = WWW_ROOT . $options['destination'] . $name;
  35.                     if (move_uploaded_file($Model->data[$Model->alias][$field]['tmp_name'], $path)) {
  36.                         $Model->data[$Model->alias][$field] = $options['destination'] . $name;
  37.                     } else {
  38.                         $Model->invalidate($field, __('Sorry, the file could not be uploaded.', true));
  39.                         return false;
  40.                     }
  41.                     break;
  42.                 }
  43.             }
  44.            
  45.             if (!$supported) {
  46.                 $Model->invalidate($field, __('Sorry, this file type is not supported.', true));
  47.             }
  48.         }
  49.         return ife(count($Model->validationErrors) > 0, false, true);
  50.     }
  51.    
  52. }
  53. ?>
Parsed in 0.097 seconds, using GeSHi 1.0.7.14

Modify this Paste