04.03
php
saved
ChrisPartridge
Note
Upload Behavior
Upload Behavior
- <?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) {
- foreach($this->__fields as $field=>$options) {
- // Check for upload
- 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) {
- $matches++;
- }
- }
- if($matches == 0) {
- continue;
- }
- }
- // Create final save path
- if(!$options['random_filename']) {
- $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;
- }
- // Update model data
- $model->data[$model->name][$field] = $saveAs;
- }
- }
- }
- }
- ?>
Parsed in 0.230 seconds, using GeSHi 1.0.7.14