03.12
php
saved
bxtox
Note
Multiple Upload Behavior
Multiple Upload Behavior
- <?php
- /**
- * Improved Multiple Upload Behaviour
- * This behaviour is based on Chris Partridge's upload behaviour (http://bin.cakephp.org/saved/17539)
- * @author Tane Piper (digitalspaghetti@gmail.com)
- * @link http://www.digitalspaghetti.me.uk
- * @filesource http://bakery.cakephp.org/articles/view/improved-upload-behaviour-with-thumbnails-and-name-correction
- * @version 1.2.1
- * @modifiedby $LastChangedBy: dalpo85@gmail.com
- * @lastmodified $Date:2008/07/29$
- * @svn $Id:$
- *
- * Version Details
- * 1.2.2
- * + Rewritten dalpo
- *
- *
- * 1.2
- * + Rewritten AD7six
- *
- * 1.1
- * + Improved Image scaling code
- * + Fixed check to see if file exists and rename file to unique name
- * + Improved model actsAs to allow more thumbnail sizes
- *
- * 1.0
- * + Initial release with thumbnail code.
- */
- class MultipleUploadBehavior extends ModelBehavior {
- 'field' => 'photo',
- 'overwriteExisting' => false,
- 'deleteMainFile' => false,
- 'createDirectory' => true,
- 'randomFilenames' => true,
- 'small' => array('width' => 100, 'height' => 100, 'name' => '{$file}.small.{$ext}', 'autoResize' => true),
- 'medium' => array('width' => 220, 'height' => 220, 'name' => '{$file}.medium.{$ext}', 'autoResize' => true),
- 'large' => array('width' => 800, 'height' => 600, 'name' => '{$file}.large.{$ext}', 'autoResize' => true)
- ),
- 'dir' => '{APP}uploads{DS}{$class}{DS}{$foreign_id}',
- //'/&(.)(tilde);/' => "$1y", // ñs
- //'/&(.)(uml);/' => "$1e", // umlauts but umlauts are not pronounced the same is all languages.
- //'/�/' => 'ss', // German double s
- //'/&(.)elig;/' => '$1e', // ae and oe symbols
- //'/�/' => 'eth' // Icelandic eth symbol
- //'/�/' => 'thorn' // Icelandic thorn
- '/&(.)(acute|caron|cedil|circ|elig|grave|horn|ring|slash|th|tilde|uml|zlig);/' => '$1', // strip all
- 'decode' => true, // html decode at this point
- '/\&/' => ' and ', // Ampersand
- '/\+/' => ' plus ', // Plus
- '/([^a-z0-9\.]+)/' => '_', // None alphanumeric
- '/\\_+/' => '_' // Duplicate sperators
- )
- )
- );
- $settings = am ($this->__defaultSettings, $config);
- uses('folder');
- $this->settings[$model->name] = $settings;
- }
- /**
- * Before Save Method..
- *
- * @param object $model
- * @return boolean
- */
- function beforeSave(&$model) {
- $error = true;
- foreach ($this->settings[$model->name] as $keyName => $fileValues) {
- if($keyName == 'defaultSettings') continue;
- // Check for upload
- continue;
- }
- if($model->data[$model->name][$field]['error'] == 4) {
- continue;
- }
- // Check it's a file submission
- debug($field.' is not an array\nForm must be multipart/form-data');
- $error = false;
- break;
- }
- // Check error
- if($model->data[$model->name][$field]['error'] > 0) {
- debug('Not valid file\nerror in upload data');
- $error = false;
- break;
- }
- // Check mime
- debug($field.' > '.$model->data[$model->name][$field]['type'].' is not a valid file\nerror in mime type');
- $error = false;
- break;
- }
- // Check extensions
- debug($field.' is not a valid file\nerror with extension '. $extension);
- $error = false;
- break;
- }
- // Get filename
- $filename = $this->_getFilename($model, $fileValues, $filename);
- $model->data[$model->name][$field]['name'] = $filename.'.'.$extension;
- // Get file path
- $dir = $this->_getPath($model, $fileValues, $dir);
- if (!$dir) {
- debug('couldn\'t determine or create directory for the field '.$field);
- $error = false;
- break;
- }
- // Create final save path
- $saveAs = $dir . DS . $model->data[$model->name][$field]['name'];
- // Check if file exists
- $saveAs = $dir . DS . $model->data[$model->name][$field]['name'];
- }
- }
- // Attempt to move uploaded file
- debug('could not move file');
- $error = false;
- break;
- }
- 'dir' => $dir,
- 'filename' => $model->data[$model->name][$field]['name'],
- 'saveas' => $saveAs
- );
- }
- // If there are errors delete all uploaded files
- if(!$error) {
- foreach ($uploadedFiles as $file) {
- }
- return false;
- }
- //if all files are uploaded then
- foreach ($this->settings[$model->name] as $keyName => $fileValues) {
- if($keyName == 'defaultSettings') continue;
- // Check for upload
- if(!isset($model->data[$model->name][$field]) || $model->data[$model->name][$field]['error'] == 4) continue;
- //on Edit or Update delete old files
- foreach ($thumbsizes as $key => $thumbsize) {
- //unlink($this->_getPath($model,$dir).DS.$this->getThumbname($model, $key, $fileName[$model->name][$field]));
- if(file_exists($this->_getPath($model,$fileValues,$dir).DS.$this->getThumbname($model, $fileValues, $key, $oldFileName[$model->name][$field]))
- && !is_dir($this->_getPath($model,$fileValues,$dir).DS.$this->getThumbname($model, $fileValues, $key, $oldFileName[$model->name][$field]))) {
- unlink($this->_getPath($model,$fileValues,$dir).DS.$this->getThumbname($model, $fileValues, $key, $oldFileName[$model->name][$field]));
- }
- }
- }
- }
- // Create thumbnail of uploaded image
- // This is hard-coded to only support JPEG + PNG + GIF at this time
- foreach ($thumbsizes as $key => $value) {
- $thumbName = $this->getThumbname ($model, $fileValues, $key, $uploadedFiles[$keyName]['filename']);
- $this->createthumb($model, $uploadedFiles[$keyName]['saveas'], $uploadedFiles[$keyName]['dir'] . DS . $thumbName, $value['width'], $value['height'], $value['autoResize']);
- }
- }
- // Update model data
- $model->data[$model->name]['mimetype'] = $model->data[$model->name][$field]['type'];
- $model->data[$model->name]['filesize'] = $model->data[$model->name][$field]['size'];
- $model->data[$model->name][$field] = $model->data[$model->name][$field]['name'];
- //if deleteMainFile = true then delete it and keep only thumbnails
- }
- }
- // pr($model->data);
- // die('beha');
- }
- function beforeDelete(&$model) {
- foreach ($this->settings[$model->name] as $keyName => $fileValues) {
- if($keyName == 'defaultSettings') continue;
- foreach ($thumbsizes as $key => $thumbsize) {
- $dFile = $this->_getPath($model,$fileValues,$dir).DS.$this->getThumbname($model, $fileValues, $key, $fileName[$model->name][$field]);
- }
- }
- $dFile = $this->_getPath($model,$fileValues,$dir).DS.$fileName[$model->name][$field];
- }
- }
- }
- // Function to create thumbnail image
- function createthumb(&$model, $name, $filename, $new_w, $new_h, $autoResize = true) {
- $src_img = imagecreatefromjpeg($name);
- }
- $src_img = imagecreatefrompng($name);
- }
- $src_img = imagecreatefromgif($name);
- }
- $old_x = imagesx($src_img);
- $old_y = imagesy($src_img);
- $model->data[$model->name]['width'] = $old_x;
- $model->data[$model->name]['height'] = $old_y;
- // //return false if the thumbnails is greater than image..
- // if (($old_x < $new_w) || ($old_y < $new_h)) {
- //
- // return false;
- //
- // }
- //calculate the new dimensions proportionately
- if($autoResize) {
- if ($old_x >= $old_y) {
- $thumb_w = $new_w;
- $ratio = $old_y / $old_x;
- $thumb_h = $ratio * $new_w;
- } else if ($old_x < $old_y) {
- $thumb_h = $new_h;
- $ratio = $old_x / $old_y;
- $thumb_w = $ratio * $new_h;
- }
- } else {
- $thumb_w = $new_w;
- $thumb_h = $new_h;
- }
- $dst_img = imagecreatetruecolor($thumb_w, $thumb_h);
- imagecopyresampled($dst_img, $src_img, 0, 0, 0, 0, $thumb_w, $thumb_h, $old_x, $old_y);
- imagepng($dst_img, $filename);
- }
- imagegif($dst_img, $filename);
- }
- else {
- imagejpeg($dst_img, $filename);
- }
- imagedestroy($dst_img);
- imagedestroy($src_img);
- }
- function getThumbname ($model, $fileValues, $thumbsize, $filename, $extension = null) {
- if ($extension == null ) {
- }
- $mergedSettings = am($this->settings[$model->name]['defaultSettings']['thumbsizes'],$fileValues['thumbsizes']);
- return $name.$filename.'.'.$extension;
- }
- }
- function getThumbSizes(&$model, $size = null) {
- if ($size) {
- return $thumbsizes[$size];
- }
- return $thumbsizes;
- }
- function initDir(&$model, $dirToCheck = null) {
- if ($dirToCheck) {
- $dir = $dirToCheck;
- }
- // Check if directory exists and create it if required
- if($create_directory && !$this->Folder->mkdirr($dir)) {
- }
- }
- // Check if directory is writable
- }
- // Check that the given directory does not have a DS on the end
- }
- }
- /**
- * return the cleaned filename (without the file extension)
- *
- * @param object $model
- * @param array $fileValues
- * @param string $string
- * @return string
- */
- function _getFilename($model, $fileValues, $string) {
- if ($randomFilenames) {
- }
- foreach ($nameCleanups as $regex => $replace) {
- if ($regex == 'decode') {
- }
- else {
- }
- }
- return $string;
- }
- /**
- * return the absolute file path
- *
- * @param object $model
- * @param array $fileValues
- * @param string $path
- * @return string
- */
- function _getPath ($model, $fileValues, $path) {
- return $path;
- }
- new Folder ($folderPath, true);
- return $folderPath;
- }
- //validation function
- /**
- * Validate an uploaded file
- *
- * @return boolean
- */
- function validateUploadedFile($model) {
- $eachArray = null;
- $upload_info = ($model->data[$model->name]);
- $field = $eachArray[0];
- $upload_info = $upload_info[$field];
- if ($upload_info['error'] == 4) {
- return true;
- }
- if ($upload_info['error'] !== 0) {
- return false;
- }
- }
- /**
- * Check the file extension of an uploaded file
- *
- * @param unknown_type $data
- * @param unknown_type $extensions
- * @return unknown
- */
- function validateFileExtension($model, $data, $extensions) {
- $eachArray = null;
- $upload_info = ($model->data[$model->name]);
- $field = $eachArray[0];
- $upload_info = $upload_info[$field];
- if($upload_info['error']==4) { return true; }
- $filename = low($upload_info['name']);
- }
- /**
- * Check the file size of an uploaded file
- *
- * @param unknown_type $data
- * @param unknown_type $extensions
- * @return unknown
- */
- function maxFileSize($model, $data, $fileSize) {
- $upload_info = ($model->data[$model->name]);
- $field =$eachArray[0];
- $upload_info = $upload_info[$field];
- if ($upload_info['size'] > $fileSize) {
- return false;
- } else {
- return true;
- }
- }
- /**
- * Validate an uploaded file
- *
- * @return boolean
- */
- function requiredFile($model, $required = true) {
- $eachArray = null;
- $upload_info = ($model->data[$model->name]);
- $field = $eachArray[0];
- $upload_info = $upload_info[$field];
- if ($upload_info['error'] == 4 && $required) {
- return false;
- }
- if ($upload_info['error'] !== 0) {
- return false;
- }
- if ($upload_info['size'] == 0) {
- return false;
- }
- return true;
- }
- }
- ?>
- <?php
- //example
- class Company extends AppModel {
- var $name = 'Company';
- 'field' => 'photo',
- 'dir' => "{IMAGES}company",
- 'deleteMainFile' => true,
- 'main' => array('width' => 350, 'height' => 350, 'name' => 'main.{$file}.{$ext}', 'autoResize' => true)
- )
- )
- )
- );
- 'message' => 'File non valido',
- 'required' => true
- ),
- 'message' => 'File non valido, inserire solo file jpg/jpeg, gif o png'
- ),
- 'message' => 'File non valido, inserire file di dimensioni inferiori ai 2 MegaByte'
- )
- )
- }
- ?>
Parsed in 0.923 seconds, using GeSHi 1.0.7.14