A file uploader and thumbnailer component for CakePHP 1.3 and 2.
OTHER License
Attachment component for CakePHP handles file uploads to the file system. If it's an image file, it creates thumbnail copies in @/app/webroot/attachments/photos/{required_sizes}@ folder; while other files are stored in @/app/webroot/attachments/files@.
I've started it from the public copy at "@http://sabbour.wordpress.com/2008/07/18/enhanced-image-upload-component-for-cakephp-12/@":http://sabbour.wordpress.com/2008/07/18/enhanced-image-upload-component-for-cakephp-12/ to have a standard way of uploading files to CakePHP projects.
See branch cake1.3 for Cake 1.3 version; branch cake2 for CakePHP 2 version.
h2. Requirements
h2. Installation
and {prefix}_content_type to the model. The prefix is the model name, in lowercase, words seperated by underscores.
h2. Model setup
We'll asume a Model named 'Pet' for a sample installation. The code relies on four columns in the Model table:
@pet_file_path, pet_file_name, pet_file_size@ and @pet_content_type@
Models with multi-word names should have each word seperated by underscores. For example, the model "GalleryPhoto" would use the table "gallery_photos" and the following fields:
@gallery_photo_file_path, gallery_photo_file_name, gallery_photo_file_size@ and @gallery_photo_content_type@
These fields are automagically updated when you call @save()@ on the @$this->data@ array
h2. View setup
h3. On forms
@@ @@
The (file) input 'pet' is the lower case model-name, multi-word model names seperated by underscores.
h3. Show files (after they are saved)
For images: @@ For files: @@
h2. Controller setup
Configuration options (default values between parentheses):
You can override the default configuration passing an array of options while including the component, like:
@var $components = array('Attachment' => array( 'files_dir' => 'pets', 'images_size' => array( 'avatar' => array(75, 75, 'resizeCrop') ) ));@
h3. Controller methods:
h4. upload($data)
Simply call the following on the form data:
@$this->Attachment->upload($this->data['Pet']);@
You may choose different column prefixes than the model name, as long as you specify it on the upload method, like so:
@$this->Attachment->upload($this->data['Pet'], 'dog');@
h4. thumbnail($data, $upload_dir, $maxw, $maxh, $crop = 'resize')
Used by the more general @upload@ method.
h4. delete_files($filename)
h2. Validations
You may wish to validate from the model. e.g:
@var $validate = array(@ @'pet_file_size' => array(@ @'rule' => array('maxLength', 6),@ @'message' => 'Image size is waaaaaayyy too big. Try resizing first'@ @)@ @);@
h3. Show validation errors in the view
@if(isset($this->Form->validationErrors)) {@ @foreach ($this->Form->validationErrors as $model => $columns) {@ @foreach ($columns as $err_msg) {@ @echo $this->Html->div('error-message', $err_msg);@ @}@ @}@ @}@
h4. Only for the file input:
@if (isset($this->Form->validationErrors['Pet']['pet_file_name'])) {@ @echo $this->Html->div('error-message', $this->Form->validationErrors['Pet']['pet_file_name']);@ @}@