Assembla home | Assembla project page
 

Changeset 66

Show
Ignore:
Timestamp:
06/14/07 12:47:32 (1 year ago)
Author:
digitalspaghetti
Message:

Improved Upload Behaviour

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • branch/models/behaviors/upload.php

    r65 r66  
    33/** 
    44 *      Improved Upload Behaviour 
     5 *      This behaviour is based on Chris Partridge's upload behaviour (http://bin.cakephp.org/saved/17539) 
     6 *      @author Tane Piper (digitalspaghetti@gmail.com) 
     7 *      @link http://www.digitalspaghetti.me.uk 
     8 *      @filesource http://bakery.cakephp.org/articles/view/improved-upload-behaviour-with-thumbnails-and-name-correction 
     9 *      @version 1.1 
     10 *      @modifiedby      $LastChangedBy:$ 
     11 *      @lastmodified    $Date:$ 
     12 *      @svn             $Id:$ 
     13 *  
     14 *      Useage: 
     15 *      1) Download this behaviour and place it in your models/behaviours/upload.php 
     16 *      2) If you require thumbnails for image generation, download Nate's phpThumb Component (http://bakery.cakephp.org/articles/view/phpthumb-component) 
     17 *      3) Insert the following SQL into your database.  This is a basic model you can expand on: 
     18 *                      CREATE TABLE `images` ( 
     19 *                              `id` int(8) unsigned NOT NULL auto_increment, 
     20 *                              `filename` varchar(255) default NULL, 
     21 *                              `dir` varchar(255) default NULL, 
     22 *                              `mimetype` varchar(255) NULL, 
     23 *                              `filesize` int(11) unsigned default NULL, 
     24 *                              `created` datetime default NULL, 
     25 *                              `modified` datetime default NULL, 
     26 *                              PRIMARY KEY  (`id`) ) ENGINE=MyISAM  DEFAULT CHARSET=utf8;  
     27 *      4) In your model that you want to have the upload behavior work, place the below code.  This example is for an Image model: 
     28 *  
     29 *      var $actsAs = array('Upload' => array( 
     30 *                              'filename' => array( 
     31 *                                              'dir' => 'files/images', 
     32 *                                              'overwrite_existing' => false, 
     33 *                                              'create_directory' => false, 
     34 *                                              'allowed_mime' => array('image/jpeg', 'image/pjpeg', 'image/gif', 'image/png'), 
     35 *                                              'allowed_ext' => array('.jpg', '.jpeg', '.png', '.gif'), 
     36 *                                              'thumbsizes' => array( 
     37 *                                                                              'small' =>      array('width'=>100, 'height'=>100), 
     38 *                                                                              'medium'=>      array('width'=>220, 'height'=>220), 
     39 *                                                                              'large' =>      array('width'=>800, 'height'=>600) 
     40 *                                              ) 
     41 *                              ) 
     42 *                      ) 
     43 *      ); 
     44 *      The above code will save the uploaded file's name in the 'filename' field in database, 
     45 *      it will not overwrite existing files, instead it will create a random filename if 
     46 *  it already exists. 
     47 *      Allowed Mimetypes and extentions should be pretty explanitory 
     48 *      For thumbnails, when the file is uploaded, it will create 3 thumbnail sizes and prepend the name 
     49 *      to the thumbfiles (i.e. image_001.jpg will produced thumb.small.image_001.jpg, thumb.medium.image_001.jpg, etc) 
     50 *  
     51 *      5) Create your upload view, make sure it's a multipart/form-data form, and the filename field is of type $form->file 
     52 *      6) Make sure your directory is at least CHMOD 775, also check your php.ini MAX_UPLOAD_SIZE is enough to support the filesizes you are uploading 
     53 *  
     54 *      Version Details 
     55 *  
     56 *      1.1 
     57 *      + Improved Image scaling code 
     58 *      + Fixed check to see if file exists and rename file to unique name 
     59 *      + Improved model actsAs to allow more thumbnail sizes 
     60 *  
     61 *      1.0 
     62 *      + Initial release with thumbnail code. 
    563 */ 
    664         
     
    122180                                                if(substr($model->data[$model->name][$field]['name'],-strlen($extension)) == $extension) { 
    123181                                                        $matches++; 
     182                                                        $keepext = $extension; 
    124183                                                } 
    125184                                        } 
     
    136195                                 
    137196                                // Check if file exists 
    138                                 if(file_exists($saveAs)) { 
    139                                         if(!$options['overwrite_existing'] || !unlink($saveAs)) { 
    140                                                 unset($model->data[$model->name][$field]); 
    141                                                 continue; 
    142                                         } else { 
    143                                                 srand((double)microtime()*1000000);  
    144                                                 $saveAs = $options['dir'] . DS . rand(0,100) . $model->data[$model->name][$field]['name'] 
    145                                         } 
    146                                 } 
     197                if(file_exists($saveAs)) { 
     198                    if(!$options['overwrite_existing'] || !unlink($saveAs)) { 
     199                                                $model->data[$model->name][$field]['name'] = uniqid("") . $keepext; 
     200                        $saveAs = $options['dir'] . DS . $model->data[$model->name][$field]['name']; 
     201                    } 
     202                }  
    147203                                 
    148204                                // Attempt to move uploaded file 
     
    155211                                // This is hard-coded to only support JPEG + PNG at this time 
    156212                                // Code unable to handle other formats 
    157                                 if (in_array($model->data[$model->name][$field]['type'], array('image/jpeg', 'image/pjpeg', 'image/png'))) 
     213                                if (count($options['allowed_ext']) > 0 && in_array($model->data[$model->name][$field]['type'], array('image/jpeg', 'image/pjpeg', 'image/png'))) 
    158214                                { 
    159215                                        foreach ($options['thumbsizes'] as $key => $value) :  
  • branch/models/image.php

    r63 r66  
    77                                'filename' => array( 
    88                                                'dir' => 'files/images', 
    9                                                 'overwrite_existing' => true, 
     9                                                'overwrite_existing' => false, 
    1010                                                'create_directory' => true, 
    1111                                                'allowed_mime' => array('image/jpeg', 'image/pjpeg', 'image/gif', 'image/png'), 
  • branch/models/post.php

    r60 r66  
    5252                                                'conditions' => '', 
    5353                                                'fields' => '', 
    54                                                 'order' => '', 
     54                                                'order' => 'Tag.tag ASC', 
    5555                                                'limit' => '', 
    5656                                                'offset' => '', 
     
    6969         
    7070        return true; 
    71     }  
     71    } 
     72     
     73    function publish($id) 
     74    { 
     75        $this->id = $id; 
     76        $this->savefield('published', 1); 
     77    } 
     78     
     79        function unpublish($id) 
     80    { 
     81        $this->id = $id; 
     82        $this->savefield('published', 0); 
     83    } 
    7284} 
    7385?> 
  • branch/plugins

    • Property svn:ignore set to
      tree
  • branch/tmp/cache/persistent

    • Property svn:ignore changed from
      class.paths.php
      to
      class.paths.php
      conf.component.data.php