04.03 php saved
ChrisPartridge
Tags add more
 
Note
Upload Behavior
  1. <?php
  2.    
  3. uses('folder');
  4. uses('file');
  5.  
  6. class UploadBehavior extends ModelBehavior {
  7.    
  8.     var $default_options = array('dir' => '',
  9.                                  'allowed_mime' => array(),
  10.                                  'allowed_ext' => array(),
  11.                                  'overwrite_existing' => false,
  12.                                  'create_directory' => true);
  13.                                 
  14.     var $__fields = array();
  15.  
  16.     function setup(&$model, $config=array()) {
  17.        
  18.         //$this->File = &new File;
  19.         $this->Folder = &new Folder;
  20.        
  21.         foreach($config as $field => $options) {
  22.            
  23.             // Check if given field exists
  24.             if(!$model->hasField($field)) {
  25.                 unset($config[$field]);
  26.                 unset($model->data[$model->name][$field]);
  27.             }
  28.            
  29.             // Merge given options with defaults
  30.             $options = array_merge($this->default_options, $options);
  31.            
  32.             // Generate temporary directory if none provided
  33.             if(empty($options['dir'])) {
  34.                 $options['dir'] = 'img' . DS . 'uploads' . DS . $model->name;
  35.             }
  36.            
  37.             // Check if directory exists and create it if required
  38.             if(!is_dir($options['dir'])) {
  39.                 if($options['create_directory'] && !$this->Folder->mkdirr($options['dir'])) {
  40.                     unset($config[$field]);
  41.                     unset($model->data[$model->name][$field]);
  42.                 }
  43.             }
  44.            
  45.             // Check if directory is writable
  46.             if(!is_writable($options['dir'])) {
  47.                 unset($config[$field]);
  48.                 unset($model->data[$model->name][$field]);
  49.             }
  50.            
  51.             // Check that the given directory does not have a DS on the end
  52.             if($options['dir'][strlen($options['dir'])-1] == DS) {
  53.                 $options['dir'] = substr($options['dir'],0,strlen($options['dir'])-2);
  54.             }
  55.                
  56.         }
  57.        
  58.         $this->__fields = $config
  59.     }
  60.    
  61.    
  62.     function beforeSave(&$model) {
  63.         if(count($this->__fields) > 0) {
  64.             foreach($this->__fields as $field=>$options) {
  65.                
  66.                 // Check for upload
  67.                 if(isset($model->data) && !is_array($model->data[$model->name][$field])) {
  68.                     unset($model->data[$model->name][$field]);
  69.                     continue;
  70.                 }
  71.                    
  72.                 // Check error
  73.                 if($model->data[$model->name][$field]['error'] > 0) {
  74.                     unset($model->data[$model->name][$field]);
  75.                     continue;
  76.                 }
  77.                
  78.                 // Check mime
  79.                 if(count($options['allowed_mime']) > 0 && !in_array($model->data[$model->name][$field]['type'], $options['allowed_mime'])) {
  80.                     unset($model->data[$model->name][$field]);
  81.                     continue;
  82.                 }
  83.                
  84.                 // Check extensions
  85.                 if(count($options['allowed_ext']) > 0) {
  86.                    
  87.                     $matches = 0;
  88.                     foreach($options['allowed_ext'] as $extension) {
  89.                         if(substr($model->data[$model->name][$field]['name'],-strlen($extension)) == $extension) {
  90.                             $matches++;
  91.                         }
  92.                     }
  93.                    
  94.                     if($matches == 0) {
  95.                         unset($model->data[$model->name][$field]);
  96.                         continue;
  97.                     }
  98.                    
  99.                 }
  100.                
  101.                 // Create final save path
  102.                 if(!$options['random_filename']) {
  103.                     $saveAs = $options['dir'] . DS . $model->data[$model->name][$field]['name'];
  104.                 } else {
  105.                     $uniqueFileName = uniqid("");
  106.                     $saveAs = $options['dir'] . DS . $uniqueFileName;
  107.                 }
  108.                
  109.                 // Check if file exists
  110.                 if(file_exists($saveAs)) {
  111.                     if(!$options['overwrite_existing'] || !unlink($saveAs)) {
  112.                         unset($model->data[$model->name][$field]);
  113.                         continue;
  114.                     }
  115.                 }
  116.                
  117.                 // Attempt to move uploaded file
  118.                 if(!move_uploaded_file($model->data[$model->name][$field]['tmp_name'], $saveAs)) {
  119.                     unset($model->data[$model->name][$field]);
  120.                     continue;
  121.                 }
  122.                
  123.                 // Update model data
  124.                 $model->data[$model->name][$field] = $saveAs;
  125.            
  126.             }
  127.         }
  128.     }
  129.    
  130.    
  131. }
  132.    
  133. ?>
Parsed in 0.238 seconds, using GeSHi 1.0.7.14

Modify this Paste