06.30
php
saved
Linnk
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.
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.
- <?php
- /**
- * 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.
- *
- * @author Dan Sondergaard <dan1990@gmail.com>
- * @copyright Copyright(c) 2008, Dan Sondergaard
- * @version 0.1
- */
- class FileBehavior extends ModelBehavior {
- /**
- * Initiate behavior for the model using specified settings. Settings are
- * specified as an array with the following structure:
- *
- * <code>
- * var $actsAs = array(
- * 'File' => array(
- * 'fieldName' => array(
- * '/regex/' => array(
- * 'destination' => 'path/to/save/to',
- * 'naming' => array('userSpecifiedFunction'),
- * 'max' => 1024,
- * 'extension' => true
- * )
- * ),
- * 'file' => array(
- * '/application\/pdf/' => array(
- * 'destination' => 'files/documents/',
- * 'naming' => array('hash'),
- * 'max' => 100,
- * 'extension' => true
- * )
- * )
- * )
- * );
- * </code>
- *
- * @param object $Model Model using the behavior
- * @param array $settings Settings to override for model.
- * @access public
- */
- 'destination' => 'files/',
- 'max' => 5242880,
- 'naming' => 'hash',
- 'extension' => true
- )
- )
- );
- }
- $this->settings[$Model->alias],
- );
- }
- /**
- * Saves uploaded files to locations given in the options for each
- * mime-type, and provides basic validation for file size. Also takes
- * care of naming by calling a user-specified method (defaults to
- * Model::hash()).
- *
- * @param object $Model Model using the behavior
- * @access public
- */
- function beforeSave(&$Model) {
- foreach ($this->settings[$Model->alias] as $field => $types) {
- foreach ($types as $pattern => $options) {
- $supported = true;
- // Get the name of the file using a callback function defined
- // in the model. Defaults to a function called hash(). The first
- // argument is the file array generated by PHP.
- $options['naming'] = $options['naming'][0];
- $name = $Model->{$options['naming']}(&$Model->data[$Model->alias][$field]);
- } else {
- $name = $options['naming'];
- }
- // Checks if the file is larger than allowed. The variable
- // $options['max'] defaults to 0, which means that there is no limit.
- if ($options['max'] > 0 && $Model->data[$Model->alias][$field]['size'] >= $options['max']) {
- $Model->invalidate($field, __('Sorry, this file is too big.', true));
- }
- // Check if we're supposed to append the extension to the unique file name.
- $filename = ife($options['extension'], $name . '.' . $info['extension'], $name);
- $path = WWW_ROOT . $options['destination'];
- }
- $Model->data[$Model->alias][$field] = '/' . $options['destination'] . $filename;
- } else {
- return false;
- }
- break;
- }
- }
- if (!$supported) {
- $Model->invalidate($field, __('Sorry, this file type is not supported.', true));
- }
- }
- }
- /**
- * Deletes files when the record they belong to is deleted.
- *
- * @param object $Model Model using the behavior
- * @access public
- */
- function beforeDelete(&$Model) {
- foreach ($this->settings[$Model->alias] as $field => $types) {
- $Model->data = $Model->read();
- }
- return true;
- }
- }
- ?>
Parsed in 0.155 seconds, using GeSHi 1.0.7.14