A file uploader and thumbnailer component for CakePHP
Pull request Compare This branch is 1 commit ahead, 32 commits behind tute:master.
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.


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
to have a standard way of uploading files to CakePHP projects.


  1. Copy attachment.php to /app/controllers/components/
  2. Add component to your controller: var $components = array('Attachment');
  3. In your upload form:
    1. echo $form->create('Model', array('type' => 'file'));
    2. echo $form->file('Attachment');

Filesystem storage (default)

  1. In your controller action (save file_path):
    $file_path = $this->Attachment->upload($this->data['Model']['Attachment']);
  2. In your view, if it’s an image:
    <?= $html->image('/attachments/$folderName/{size}/'.$data['Model']['file_path']); ?>

Database storage

  1. Change $config['database'] = true in attachment.php.
  2. In your controller action:
    $binary_data = $this->Attachment->upload($this->data['Model']['Attachment']);
  3. Save $binary_data to 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.)



  • 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)


  • filename: file-to-delete name


  • 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_path returned by
    $this->Attachment->upload in order to reference the file.

To do list

  • Image resize in upload_DB.