CakePHP uploads linked to Models

How to upload files such as images for a certain Model in CakePHP, you ask?

Get the Upload plugin

Simply download the following plugin to your /app/Plugin/ folder, and make sure the new folder in there is called ‘Upload’.
Enable the plugin in your /app/Config/bootstrap.php;


You can also just follow the Installation instructions on the Plugin website.

Use the plugin in your Model of choice

In the model you want to have uploads, create a database field called ‘photo’ (varchar 255) and add the following code to your Model;

    public $actsAs = array(
        'Upload.Upload' => array(
            'photo' => array(
                'thumbnailSizes' => array(
                    'vga' => '285x285',
                    'thumb' => '80x80'
                'thumbnailMethod'  => 'php',
                'fields' => array(
                	'photo' => 'photo',
                	'dir' => 'uploads',
                	'type' => 'photo_type'

Then in the View of the add/edit page, add the following code:

echo $this->Form->create('Note', array('class' => 'form-horizontal', 'type' => 'file'));
echo $this->Form->input('', array('type' => 'file', 'label' => 'Foto'));
echo $this->Form->input('message', array('label' => __('Bericht')));
echo $this->Form->end(); 

That’s it!

By default, the new uploads are saved in the /app/webroot/files/{modelname}/photo/{model_entry_id}. You can change this in the options in the Model.
More about those options here.

Happy coding!