download

Ajax File Upload using jQuery and CakePHP Media Plugin

| Updated: August 2, 2011

Cakephp Media Plugin is one of the best PHP file upload platform or plugin that is out on the web right now. Although this plugin is not yet done but we can already use its beta and it works fine, I'm using it for a while now (from 0.6 to 1.3 betas).

Throughout the tutorial I would like to introduce to you the combination of cakephp and jquery for simple yet powerful File upload.

Step 1: Download all the necessary files

  1. Download the updated version of CakePHP 1.3 (http://www.cakephp.org)
  2. Download David Persson’s  Cakephp Media Plugin (https://github.com/davidpersson/media)
  3. Download my jQuery Fileuploader plugin (https://github.com/johnlanz/jquery-fileuploader)

Step 2: Installation

  1. Extract cakephp media plugin and move or copy it to cakephp app plugins folder (/path/to/your/app/plugins/media)
  2. Extract fileuploader and move the js files (jquery.fileUploader.js and jquery) into cakephp js webroot folder  (/path/to/your/app/webroot/js)
    • Move fileUploader.css into ‘/path/to/your/app/webroot/css’
    • Move the images close.gif and image_upload.gif into ‘/path/to/your/app/webroot/img’
  3. Create a table that stores information about your uploaded files.
  1.  
  2. CREATE TABLE `galleries` (
  3. `id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
  4. `basename` VARCHAR( 255 ) NOT NULL,
  5. `dirname` VARCHAR( 255 ) NOT NULL ,
  6. `created` DATETIME NULL,
  7. `modified` DATETIME NULL
  8. ) ENGINE = MYISAM;
  9.  

Step 3: Media Plugin Configuration

Edit bootstrap.php located at '/app/config/' and add the following lines

  1.  
  2. require APP . 'plugins' . DS . 'media' . DS . 'config' . DS . 'core.php';
  3. //images sizes
  4. $small = array('fitCrop' => array(75, 50));
  5. $medium = array('fitCrop' => array(220, 140));
  6. $large = array('fitCrop' => array(700, 335));
  7.  
  8. Configure::write('Media.filter', array(
  9. 'audio' => array(),
  10. 'document' => array(),
  11. 'generic' => array(),
  12. 'image' => compact('small', 'medium', 'large'),
  13. 'video' => compact('medium', 'large')
  14. ));
  15.  

Step 4: Create a Model, View, and Controller for the gallery

  1. Create gallery.php model
    1.  
    2. < ?php
    3. class Gallery extends AppModel {
    4. var $name = 'Gallery';
    5. //media plugin behaviors
    6. var $actsAs = array(
    7. 'Media.Transfer',
    8. 'Media.Coupler',
    9. 'Media.Generator'
    10. );
    11. //file validation which only allowed jpeg and png to be uploaded
    12. var $validate = array(
    13. 'file' => array(
    14. 'mimeType' => array(
    15. 'rule' => array('checkMimeType', false, array( 'image/jpeg', 'image/png'))
    16. )
    17. )
    18. );
    19. }
    20. ?>
    21.  
  2. Create galleries_controller.php
    1.  
    2. < ?php
    3. class GalleriesController extends AppController {
    4.  
    5. var $name = 'Galleries';
    6. var $components = array('RequestHandler');
    7.  
    8. function add() {
    9. if (!empty($this->data)) {
    10. if ($this->Gallery->save($this->data)) {
    11. $result = '<div id="status">success</div>';
    12. $result .= '<div id="message">Successfully Uploaded</div>';
    13. } else {
    14. $result = "<div id='status'>error</div>";
    15. $result .= '<div id="message">'. $this->Gallery->validationErrors['file'] .'</div>';
    16. }
    17.  
    18. $this->RequestHandler->renderAs($this, 'ajax');
    19. $this->set('result', $result);
    20. $this->render('../elements/ajax');
    21. }
    22. }
    23.  
    24. }
    25. ?>
    26.  
    As you can see I've used request handler component which I assumed that all submitted form data came from ajax.

  3. Create ajax.ctp and put inside '/app/views/elements' folder
    1.  
    2. < ?php
    3. Configure::write('debug', 0);
    4. echo $result;
    5. ?>
    6.  
    It handles views for ajax request.
  4. Create folder galleries inside '/app/views'. And create add.ctp file inside galleries folder.
    1.  
    2. # add.ctp
    3. < ?php
    4. echo $html->script('jquery-1.6.2.min', false);
    5. echo $html->script('jquery-ui-1.8.14.custom.min', false);
    6. echo $html->script('jquery.fileUploader', false);
    7. echo $html->css('ui-lightness/jquery-ui-1.8.14.custom', null, array(), false);
    8. echo $html->css('fileUploader', null, array(), false);
    9. ?>
    10.  
    11. <div class="galleries form">
    12. <h2>< ?php __('jQuery Fileuploder Plugin');?></h2>
    13. < ?php
    14. echo $this->Form->create('Gallery', array('type' => 'file'));
    15. echo $this->Form->input('file', array(
    16. 'type' => 'file',
    17. 'label' => false, 'div' => false,
    18. 'class' => 'fileUpload',
    19. 'multiple' => 'multiple'
    20. ));
    21. echo $this->Form->button('Upload', array('type' => 'submit', 'id' => 'px-submit'));
    22. echo $this->Form->button('Clear', array('type' => 'reset', 'id' => 'px-clear'));
    23. echo $form->end();
    24. ?>
    25. </div>
    26.  
    27. < script type="text/javascript">
    28. $(function(){
    29. $('.fileUpload').fileUploader();
    30. });
    31. < /script>
    32.  

That's it. You can upload videos, documents, images, etc., and cakephp media plugin handles them perfectly.

You can download the whole tutorials here. jQuery Fileuploader and CakePHP Media Plugin

blog comments powered by Disqus