06.01
php
saved
dardosordi
Note
Tweak over ChrisPartridge's upload behaviour (http://bin.cakephp.org/saved/17539)
It's not nice but does what my boss want!
Tweak over ChrisPartridge's upload behaviour (http://bin.cakephp.org/saved/17539)
It's not nice but does what my boss want!
- <?php
- uses('folder');
- uses('file');
- class UploadBehavior extends ModelBehavior
- {
- 'overwrite_existing' => false,
- 'create_directory' => true);
- {
- //$this->File = &new File;
- $this->Folder = &new Folder;
- foreach($config as $field => $options)
- {
- // Check if given field exists
- if(!$model->hasField($field))
- {
- }
- // Merge given options with defaults
- // Generate temporary directory if none provided
- {
- $options['dir'] = 'img' . DS . 'uploads' . DS . $model->name;
- }
- // Check if directory exists and create it if required
- {
- if($options['create_directory'] && !$this->Folder->mkdirr($options['dir']))
- {
- }
- }
- // Check if directory is writable
- {
- }
- // Check that the given directory does not have a DS on the end
- {
- }
- }
- $this->__fields = $config;
- }
- function beforeSave(&$model)
- {
- // print_r($model->data); die();
- {
- foreach($this->__fields as $field=>$options)
- {
- // Check for upload
- {
- continue;
- }
- {
- continue;
- }
- // Check error
- if($model->data[$model->name][$field]['error'] > 0)
- {
- continue;
- }
- // Check mime
- {
- continue;
- }
- // Check extensions
- {
- $matches = 0;
- foreach($options['allowed_ext'] as $extension)
- {
- //if(substr($model->data[$model->name][$field]['name'],-strlen($extension)) == $extension)
- if(strtolower(substr($model->data[$model->name][$field]['name'],-strlen($extension))) == $extension)
- {
- $matches++;
- }
- }
- if($matches == 0)
- {
- continue;
- }
- }
- // Create final save path
- {
- $saveAs = $options['dir'] . DS . $model->data[$model->name][$field]['name'];
- }
- else
- {
- $saveAs = $options['dir'] . DS . $uniqueFileName;
- }
- // Check if file exists
- {
- {
- continue;
- }
- }
- // Attempt to move uploaded file
- {
- continue;
- } else {
- $this->resize($saveAs, 640, 480);
- }
- // Update model data
- $model->data[$model->name][$field] = $saveAs;
- if($model->id)
- {
- $m = new $model->name;
- $data = $m->read(null, $model->id);
- $file = $m->data[$model->name][$field];
- {
- $folder = &new Folder($dir);
- }
- }
- }
- }
- }
- function beforeDelete(&$model)
- {
- {
- $model->read(null, $model->id);
- {
- foreach($this->__fields as $field=>$options)
- {
- $file = $model->data[$model->name][$field];
- {
- $folder = &new Folder($dir);
- return false;
- }
- }
- }
- }
- return true;
- }
- function resize($filename, $width, $height, $aspect = true)
- {
- $fullpath = WWW_ROOT;
- $url = $fullpath.$filename;
- return; // image doesn't exist
- return; // image doesn't exist
- if ($aspect) { // adjust to aspect.
- if (($size[1]/$height) > ($size[0]/$width)) // $size[0]:width, [1]:height, [2]:type
- else
- }
- $relfile = $url;
- $resize = ($size[0] > $width || $size[1] > $height) || ($size[0] < $width || $size[1] < $height);
- if ($resize) {
- $tempcolor = imagecolorallocate ( $temp, $this->color[0], $this->color[1], $this->color[2] );
- imagefill($temp, 0, 0, $tempcolor);
- imagecopyresampled ($temp, $image, 0, 0, 0, 0, $width, $height, $size[0], $size[1]);
- } else {
- $temp = imagecreate ($width, $height);
- $tempcolor = imagecolorallocate ( $temp, $this->color[0], $this->color[1], $this->color[2] );
- imagefill($temp, 0, 0, $tempcolor);
- imagecopyresized ($temp, $image, 0, 0, 0, 0, $width, $height, $size[0], $size[1]);
- }
- imagedestroy ($image);
- imagedestroy ($temp);
- }
- }
- }
- ?>
Parsed in 0.436 seconds, using GeSHi 1.0.7.14