CakePHP

CakePHP 2.x Upload Plugin

2cakephp upload
Uploading a file in CakePHP is easy with plugin. Developer also can create a PHP upload script but why we need to reinvent the wheel as there is a lot of free plugin available to assist file upload in CakePHP framework. In this tutorial, i used Upload Plugin 2.0 for cakePHP 2.x by josegonzalez which i already fork here: https://github.com/Asyraf-wa/cakephp-upload

 

Quick info on Upload Plugin 2.0

  • The Upload plugin will transfer files from a form in your application to (by default) the webroot/files directory organized by the model name and primaryKey field.
  • It can also move files around programatically. Such as from the filesystem.
  • The path to which the files are saved can be customized.
  • It can also create thumbnails for image files if the thumbnails option is set in the behavior options.
  • The plugin can also upload multiple files at the same time to different fields.
  • Each upload field can be configured independently of each other, such as changing the upload path or thumbnail options.
  • Uploaded file information can be stored in a data store, such as a MySQL database.
  • A variety of validation rules are provided to help validate against common rules.


Download Plugin
Download plugin from Github link: https://github.com/Asyraf1985/cakephp-upload (download as zip archive). Extract zip file into … \app\Plugin and rename default folder name “cakephp-upload-master” to “Upload”.

Load Plugin
To load the upload plugin, navigate to … \app\Config and open file bootstrap.php and insert load plugin code as shown below (Usually at last line):

CakePlugin::load('Upload');

 

Upload Form View
Assume the upload form is document table which contain:

  1. id -pk, int (11)
  2. document - varchar (255) - store document name
  3. document_dir - varchar (255) - store document directory (for delete file purpose)
  4. size - varchar (50) - store document size
  5. type - varchar (50) - store type of file / mime / extension


To upload a file, the form header should included 'type' => 'file' and document_dir, size and type is hide from view as shown below:

<div class="documents form">
<?php echo $this->Form->create('Document', array('type' => 'file')); ?>
echo $this->Form->input('id');

//Non bootstrap use this code:
echo $this->Form->input('Document.document ', array(
'type' => 'file')); 

//Include class if use bootstrap template. (if applicable)
<?php echo $this->Form->input('Document.document', array(
'class' => 'form-control', 
'type' => 'file'));?>

echo $this->Form->input('Document.document_dir', array(
'type' => 'hidden'));

echo $this->Form->input('Document.size', array(
'type' => 'hidden'));

echo $this->Form->input('Document.type', array(
'type' => 'hidden'));

 

Validation
To validate uploaded file based on file extension, you need to include validation rules as shown below. You also can include more than one validation rules.
*validation should be in model.

public $validate = array(
'document' => array(
'extension' => array(
'rule' => array('extension', array('pdf', 'docx', 'doc', 'xls', 'ppt', 'pptx')),
'message' => 'Sorry, file are not allow to upload' ),
),
);

 

Upload Configuration
The plugin need to be configure to identify the folder directory, mode, and fields. In this tutorial, I upload the document to ..\app\webroot\files\document directory. It will record the document name, directory, type and size. Here the sample code:
*upload configuration should be in model.

public $actsAs = array(
'Upload.Upload' => array(
'document' => array(
'fields' => array(
'dir' => 'document_dir',
'type' => 'type', 
'size' => 'size'
),
'deleteOnUpdate' => true,
'deleteFolderOnDelete' => true,
'mode' => 0777,
'path' => '{ROOT}webroot{DS}files{DS}{field}{DS}',
)
)
);

 

**Developer also can configure to create thumbnail if the uploaded files is image file. However, it required developer to install Imagick or GD Library.

Download File
To download the uploaded files, create a view for download link as shown below.

<th><?php echo __('Download'); ?></th>
<td>
<?php 
echo $this->Html->link('Download', '../files/document/' .
$document['Document']['document_dir'] . '/' .
$document['Document']['document']);
?>
</td>

 

DONE! Hope this tutorial can assist anybody that love to explore and learn CakePHP framework. Don't forget to change the PHP upload max file size if you are facing problem to upload huge files.

Author Information
Muhammad Asyraf bin Wahi Anuar
Web Administrator
Asyraf enjoys life and has great enthusiasm for all outdoors activities especially MTB! Don't Follow Me. I'm Lost Too -_-

Muhammad Asyraf bin Wahi Anuar | Copyright 2017 | Informatic Trooper