06.30 php saved
Linnk
Tags add more
Upload, behavior and mimetypes  
Note
This behavior makes it easy to upload files of any type. It validates and sorts uploaded files by field, file type, and size. Support for naming callback methods makes it easy to use different types of naming schemes.

Regular expressions are supported for mime-type matching, and multiple fields can be uploaded at the same time.

The path of the saved file will be saved in the column which is equal to the name of the form field used to upload the file.
  1. <?php
  2. /**
  3. * This behavior makes it easy to upload files of any type. It validates and
  4. * sorts uploaded files by field, file type, and size. Support for naming
  5. * callback methods makes it easy to use different types of naming schemes.
  6. *
  7. * Regular expressions are supported for mime-type matching, and multiple
  8. * fields can be uploaded at the same time.
  9. *
  10. * The path of the saved file will be saved in the column which is equal to
  11. * the name of the form field used to upload the file.
  12. *
  13. * @author Dan Sondergaard <dan1990@gmail.com>
  14. * @copyright Copyright(c) 2008, Dan Sondergaard
  15. * @version 0.1
  16. */
  17. class FileBehavior extends ModelBehavior {
  18.    
  19.     var $settings = array();
  20.    
  21.     /**
  22.      * Initiate behavior for the model using specified settings. Settings are
  23.      * specified as an array with the following structure:
  24.      *
  25.      * <code>
  26.      * var $actsAs = array(
  27.      *     'File' => array(
  28.      *         'fieldName' => array(
  29.      *             '/regex/' => array(
  30.      *         'destination' => 'path/to/save/to',
  31.      *         'naming' => array('userSpecifiedFunction'),
  32.      *         'max' => 1024,
  33.      *         'extension' => true
  34.      *       )
  35.      *     ),
  36.      *     'file' => array(
  37.      *         '/application\/pdf/' => array(
  38.      *             'destination' => 'files/documents/',
  39.      *         'naming' => array('hash'),
  40.      *         'max' => 100,
  41.      *         'extension' => true
  42.      *       )
  43.      *       )
  44.      *    )
  45.      * );
  46.      * </code>
  47.      *
  48.      * @param object $Model Model using the behavior
  49.      * @param array $settings Settings to override for model.
  50.      * @access public
  51.      */
  52.     function setup(&$Model, $settings = array()) {
  53.         if (!isset($this->settings[$Model->alias])) {
  54.             $this->settings[$Model->alias] = array(
  55.                 'file' => array(
  56.                     '/image\/(png|jpg|gif)/' => array(
  57.                         'destination' => 'files/',
  58.                         'max' => 5242880,
  59.                         'naming' => 'hash',
  60.                         'extension' => true
  61.                     )
  62.                 )
  63.             );
  64.         }
  65.         $this->settings[$Model->alias] = array_merge(
  66.              $this->settings[$Model->alias],
  67.              ife(is_array($settings), $settings, array())
  68.         );
  69.     }
  70.    
  71.     /**
  72.      * Saves uploaded files to locations given in the options for each
  73.      * mime-type, and provides basic validation for file size. Also takes
  74.      * care of naming by calling a user-specified method (defaults to
  75.      * Model::hash()).
  76.      *
  77.      * @param object $Model Model using the behavior
  78.      * @access public
  79.      */
  80.     function beforeSave(&$Model) {
  81.         foreach ($this->settings[$Model->alias] as $field => $types) {
  82.             foreach ($types as $pattern => $options) {
  83.                 if (preg_match($pattern, $Model->data[$Model->alias][$field]['type'])) {
  84.                     $supported = true;
  85.                    
  86.                     // Get the name of the file using a callback function defined
  87.                     // in the model. Defaults to a function called hash(). The first
  88.                     // argument is the file array generated by PHP.
  89.                     if (is_array($options['naming'])) {
  90.                         $options['naming'] = $options['naming'][0];
  91.                         $name = $Model->{$options['naming']}(&$Model->data[$Model->alias][$field]);
  92.                     } else {
  93.                         $name = $options['naming'];
  94.                     }
  95.                    
  96.                     // Checks if the file is larger than allowed. The variable
  97.                     // $options['max'] defaults to 0, which means that there is no limit.
  98.                     if ($options['max'] > 0 && $Model->data[$Model->alias][$field]['size'] >= $options['max']) {
  99.                         $Model->invalidate($field, __('Sorry, this file is too big.', true));
  100.                     }
  101.                    
  102.                     // Check if we're supposed to append the extension to the unique file name.
  103.                     $info = pathinfo($Model->data[$Model->alias][$field]['name']);
  104.                     $filename = ife($options['extension'], $name . '.' . $info['extension'], $name);
  105.                    
  106.                     $path = WWW_ROOT . $options['destination'];
  107.                     if (!is_dir($path)) {
  108.                         mkdir($path);
  109.                     }
  110.                    
  111.                     if (move_uploaded_file($Model->data[$Model->alias][$field]['tmp_name'], $path . $filename)) {
  112.                         $Model->data[$Model->alias][$field] = '/' . $options['destination'] . $filename;
  113.                     } else {
  114.                         return false;
  115.                     }
  116.                     break;
  117.                 }
  118.             }
  119.            
  120.             if (!$supported) {
  121.                 $Model->invalidate($field, __('Sorry, this file type is not supported.', true));
  122.             }
  123.         }
  124.         return ife(count($Model->validationErrors) > 0, false, true);
  125.     }
  126.    
  127.     /**
  128.      * Deletes files when the record they belong to is deleted.
  129.      *
  130.      * @param object $Model Model using the behavior
  131.      * @access public
  132.      */
  133.     function beforeDelete(&$Model) {
  134.         foreach ($this->settings[$Model->alias] as $field => $types) {
  135.             $Model->data = $Model->read();
  136.             unlink(WWW_ROOT . $Model->data[$Model->alias][$field]);
  137.         }
  138.         return true;
  139.     }
  140.    
  141. }
  142. ?>
Parsed in 0.155 seconds, using GeSHi 1.0.7.14

Modify this Paste