File upload and thumbnail component

by tutec
Attachment component for CakePHP handles file uploads to the file system. If it’s an image file, it creates as many thumbnail copies as you need.
I’ve this project from the public copy from sabbour, to have a standard way of uploading files to CakePHP projects.

It is version controlled at (and you can download the fresher source code from)
http://github.com/tute/Thumbnail-component-for-CakePHP
Usage
  1. [li] Copy attachment.php to /app/controllers/components/ [li] Add component to your controller: var $components = array('Attachment'); [li] In your upload form:

    View Template:


    <?php
    echo $form->create('Model', array('type' => 'file'));
    echo 
    $form->file('Attachment');
    ?>

Filesystem storage (default)


1. In your controller action (save file_path):

Controller Class:

<?php $file_path $this->Attachment->upload($this->data['Model']['Attachment']);?> 2. In your view, if it’s an image:

View Template:

<?php echo $html->image('/attachments/$folderName/{size}/'.$data['Model']['file_path']); ?>

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:

Controller Class:

<?php 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 form
* upload_dir: the name of the parent folder of the images
* maxw/maxh: maximum width/height for resizing images
* crop: indicates if image must be cropped or not (defaults to false)

delete_files($file-to-delete-name)

Requirements

  1. [li] PHP GD library installed and enabled [li] /app/webroot/ must be writable by the web server (or you can create /app/webroot/attachments/ and make writable that folder). [li] You must store in database $file_path returned by $this->Attachment->upload in order to reference the file.

To do list


Image resize in upload_DB.

Report

More on Components

Advertising

Comments

  • mauri posted on 09/15/11 08:50:45 PM
    Hi, thanks for this component. I dont understand where and how i can save the file_paths, can you explain me.
    Thanks
    • arteq posted on 09/10/12 03:58:43 PM
      Hi, thanks for this component. I dont understand where and how i can save the file_paths, can you explain me.

      Took me a moment to figure out that too :-)

      When you do:
      $this->Attachment->upload($some_data);

      then the file path (& other fields as well) is added to the $some_data array like:
      $some_data['model_file_path'];
  • aifarfa posted on 06/04/11 06:24:24 AM
    it works on thumbnail images functions.
    but what about saving multiple files?

    eg.

    create('Model', array('type' => 'file'));
    echo $form->file('Attachment.0');
    echo $form->file('Attachment.1');
    echo $form->file('Attachment.2');
    ?>
login to post a comment.