Every repository with this icon (
Every repository with this icon (
| name | age | message | |
|---|---|---|---|
| |
MIT-LICENSE | Mon Jul 06 20:18:35 -0700 2009 | |
| |
README.textile | Wed Nov 11 10:08:26 -0800 2009 | |
| |
attachment.php | Wed Nov 11 10:08:26 -0800 2009 |
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/
to have a standard way of uploading files to CakePHP projects.
Usage
- Copy
attachment.phpto/app/controllers/components/ - Add component to your controller:
var $components = array('Attachment'); - In your upload form:
echo $form->create('Model', array('type' => 'file'));echo $form->file('Attachment');
Filesystem storage (default)
- In your controller action (save
file_path):
$file_path = $this->Attachment->upload($this->data['Model']['Attachment']); - In your view, if it’s an image:
<?= $html->image('/attachments/$folderName/{size}/'.$data['Model']['file_path']); ?>
Database storage
- Change
$config['database'] = truein attachment.php. - In your controller action:
$binary_data = $this->Attachment->upload($this->data['Model']['Attachment']); - Save
$binary_datato database binary column:
$this->data['Model']['file_contents'] = $binary_data
(No image processing yet, to-do!)
Configuration options
photos_dir: The folder which will hold the files, inside/app/webroot/attachments/.database: Whether to save the images in the database or the filesystem.allow_non_image_files, self descriptive ;-) .images_size: Array of different file sizes required by your app. Each
element is itself an array, like:
'folder_name' => array($width, $height, $do_crop).
You can override the default configuration passing an array of options while
including the component, like:
var $components = array('Attachment' => array('photos_dir' => 'pets'));
(Check the $config array defined at the first lines of the component to see
default values and the syntax.)
Methods
upload($data)
data: file data array from the form. If it’s an image, it calls:
thumbnail($data, $upload_dir, $maxw, $maxh, $crop = false)
data: the image data array from the formupload_dir: the name of the parent folder of the imagesmaxw/maxh: maximum width/height for resizing imagescrop: indicates if image must be cropped or not (defaults tofalse)
delete_files($filename)
filename: file-to-delete name
Requirements
- PHP GD library installed and enabled
/app/webroot/must be writable by the web server (or you can create
/app/webroot/attachments/and make writable that folder).- You must store in database
$file_pathreturned by
$this->Attachment->uploadin order to reference the file.
To do list
- Image resize in upload_DB.







