06.01 php saved
dardosordi
Tags add more
 
Note
Tweak over ChrisPartridge's upload behaviour (http://bin.cakephp.org/saved/17539)

It's not nice but does what my boss want!
  1. <?php
  2.  
  3. uses('folder');
  4. uses('file');
  5.  
  6. class UploadBehavior extends ModelBehavior
  7. {
  8.  
  9.     var $color = array(255, 255, 255);
  10.    
  11.     var $default_options = array('dir' => '',
  12.                                  'allowed_mime' => array(),
  13.                                  'allowed_ext' => array(),
  14.                                  'overwrite_existing' => false,
  15.                                  'create_directory' => true);
  16.  
  17.     var $__fields = array();
  18.  
  19.     function setup(&$model, $config=array())
  20.     {
  21.  
  22.         //$this->File = &new File;
  23.         $this->Folder = &new Folder;
  24.  
  25.         foreach($config as $field => $options)
  26.         {
  27.  
  28.             // Check if given field exists
  29.             if(!$model->hasField($field))
  30.             {
  31.                 unset($config[$field]);
  32.                 unset($model->data[$model->name][$field]);
  33.             }
  34.  
  35.             // Merge given options with defaults
  36.             $options = array_merge($this->default_options, $options);
  37.  
  38.             // Generate temporary directory if none provided
  39.             if(empty($options['dir']))
  40.             {
  41.                 $options['dir'] = 'img' . DS . 'uploads' . DS . $model->name;
  42.             }
  43.  
  44.             // Check if directory exists and create it if required
  45.             if(!is_dir($options['dir']))
  46.             {
  47.                 if($options['create_directory'] && !$this->Folder->mkdirr($options['dir']))
  48.                 {
  49.                     unset($config[$field]);
  50.                     unset($model->data[$model->name][$field]);
  51.                 }
  52.             }
  53.  
  54.             // Check if directory is writable
  55.             if(!is_writable($options['dir']))
  56.             {
  57.                 unset($config[$field]);
  58.                 unset($model->data[$model->name][$field]);
  59.             }
  60.  
  61.             // Check that the given directory does not have a DS on the end
  62.             if($options['dir'][strlen($options['dir'])-1] == DS)
  63.             {
  64.                 $options['dir'] = substr($options['dir'],0,strlen($options['dir'])-2);
  65.             }
  66.  
  67.         }
  68.  
  69.         $this->__fields = $config;
  70.     }
  71.  
  72.  
  73.     function beforeSave(&$model)
  74.     {
  75.  
  76.         //    print_r($model->data); die();
  77.  
  78.         if(count($this->__fields) > 0)
  79.         {
  80.             foreach($this->__fields as $field=>$options)
  81.             {
  82.  
  83.                 // Check for upload
  84.  
  85.                 if(!isset($model->data[$model->name][$field]))
  86.                 {
  87.                     continue;
  88.                 }
  89.  
  90.  
  91.                 if(isset($model->data) && !is_array($model->data[$model->name][$field]))
  92.                 {
  93.                     unset($model->data[$model->name][$field]);
  94.                     continue;
  95.                 }
  96.  
  97.                 // Check error
  98.                 if($model->data[$model->name][$field]['error'] > 0)
  99.                 {
  100.                     unset($model->data[$model->name][$field]);
  101.                     continue;
  102.                 }
  103.  
  104.                 // Check mime
  105.                 if(count($options['allowed_mime']) > 0 && !in_array($model->data[$model->name][$field]['type'], $options['allowed_mime']))
  106.                 {
  107.                     unset($model->data[$model->name][$field]);
  108.                     continue;
  109.                 }
  110.  
  111.                 // Check extensions
  112.                 if(count($options['allowed_ext']) > 0)
  113.                 {
  114.  
  115.                     $matches = 0;
  116.                     foreach($options['allowed_ext'] as $extension)
  117.                     {
  118.                         //if(substr($model->data[$model->name][$field]['name'],-strlen($extension)) == $extension)
  119.                         if(strtolower(substr($model->data[$model->name][$field]['name'],-strlen($extension))) == $extension)
  120.                         {
  121.                             $matches++;
  122.                         }
  123.                     }
  124.  
  125.                     if($matches == 0)
  126.                     {
  127.                         unset($model->data[$model->name][$field]);
  128.                         continue;
  129.                     }
  130.  
  131.                 }
  132.  
  133.                 // Create final save path
  134.                 if(!isset($options['random_filename']) || !$options['random_filename'])
  135.                 {
  136.                     $saveAs = $options['dir'] . DS . $model->data[$model->name][$field]['name'];
  137.                 }
  138.                 else
  139.                 {
  140.                     $uniqueFileName = uniqid("");
  141.                     $saveAs = $options['dir'] . DS . $uniqueFileName;
  142.                 }
  143.  
  144.                 // Check if file exists
  145.                 if(file_exists($saveAs))
  146.                 {
  147.                     if(!$options['overwrite_existing'] || !unlink($saveAs))
  148.                     {
  149.                         unset($model->data[$model->name][$field]);
  150.                         continue;
  151.                     }
  152.                 }
  153.  
  154.                 // Attempt to move uploaded file
  155.                 if(!move_uploaded_file($model->data[$model->name][$field]['tmp_name'], $saveAs))
  156.                 {
  157.                     unset($model->data[$model->name][$field]);
  158.                     continue;
  159.                 } else {
  160.                     $this->resize($saveAs, 640, 480);
  161.  
  162.                 }
  163.  
  164.                 chmod($saveAs, 0644);
  165.                 // Update model data
  166.                 $model->data[$model->name][$field] = $saveAs;
  167.  
  168.                 if($model->id)
  169.                 {
  170.                     $m = new $model->name;
  171.  
  172.                     $data = $m->read(null, $model->id);
  173.                     $file = $m->data[$model->name][$field];
  174.                     if (is_file($file))
  175.                     {
  176.                         unlink($file);
  177.  
  178.                         $dir=dirname($file).'/imagecache/';
  179.                         $folder = &new Folder($dir);
  180.                         $files = $folder->find('[0-9]+x[0-9]+_'.basename($file));
  181.                         foreach($files as $f) unlink($dir.$f);
  182.  
  183.                     }
  184.  
  185.                 }
  186.             }
  187.         }
  188.     }
  189.  
  190.  
  191.     function beforeDelete(&$model)
  192.     {
  193.         if(count($this->__fields) > 0)
  194.         {
  195.             $model->read(null, $model->id);
  196.             if (isset($model->data))
  197.             {
  198.                 foreach($this->__fields as $field=>$options)
  199.                 {
  200.                     $file = $model->data[$model->name][$field];
  201.                     if(is_file($file))
  202.                     {
  203.                         $dir=dirname($file).'/imagecache/';
  204.                         $folder = &new Folder($dir);
  205.                         $files = $folder->find('[0-9]+x[0-9]+_'.basename($file));
  206.                         foreach($files as $f) unlink($dir.$f);
  207.                         if (!unlink($file))
  208.                             return false;
  209.                     }
  210.                 }
  211.             }
  212.         }
  213.         return true;
  214.     }
  215.  
  216.     function resize($filename, $width, $height, $aspect = true)
  217.     {
  218.         $types = array(1 => "gif", "jpeg", "png", "swf", "psd", "wbmp"); // used to determine image type
  219.  
  220.         $fullpath = WWW_ROOT;
  221.         $url = $fullpath.$filename;
  222.  
  223.         if (!is_file($url))
  224.             return; // image doesn't exist
  225.  
  226.         if (!($size = getimagesize($url)))
  227.             return; // image doesn't exist
  228.  
  229.         if ($aspect) { // adjust to aspect.
  230.             if (($size[1]/$height) > ($size[0]/$width))  // $size[0]:width, [1]:height, [2]:type
  231.                         $width = ceil(($size[0]/$size[1]) * $height);
  232.             else
  233.                 $height = ceil($width / ($size[0]/$size[1]));
  234.         }
  235.  
  236.  
  237.         $relfile = $url;
  238.  
  239.         $resize = ($size[0] > $width || $size[1] > $height) || ($size[0] < $width || $size[1] < $height);
  240.  
  241.         if ($resize) {
  242.             $image = call_user_func('imagecreatefrom'.$types[$size[2]], $url);
  243.             if (function_exists("imagecreatetruecolor") && ($temp = imagecreatetruecolor ($width, $height))) {
  244.  
  245.                 $tempcolor = imagecolorallocate ( $temp, $this->color[0], $this->color[1], $this->color[2] );
  246.                 imagefill($temp, 0, 0, $tempcolor);
  247.  
  248.                 imagecopyresampled ($temp, $image, 0, 0, 0, 0, $width, $height, $size[0], $size[1]);
  249.             } else {
  250.                 $temp = imagecreate ($width, $height);
  251.  
  252.                 $tempcolor = imagecolorallocate ( $temp, $this->color[0], $this->color[1], $this->color[2] );
  253.                 imagefill($temp, 0, 0, $tempcolor);
  254.  
  255.                 imagecopyresized ($temp, $image, 0, 0, 0, 0, $width, $height, $size[0], $size[1]);
  256.             }
  257.             call_user_func("image".$types[$size[2]], $temp, $url);
  258.             chmod($url, 0644);
  259.             imagedestroy ($image);
  260.             imagedestroy ($temp);
  261.         }
  262.  
  263.     }
  264.  
  265. }
  266.  
  267. ?>
Parsed in 0.436 seconds, using GeSHi 1.0.7.14

Modify this Paste