Search code examples
cakephpamazon-s3uploader

CakePHP Uploader Plugin - Behavior Setup


I am using this plugin: MileJ CakePHP Uploader which works really well however I can only get it to work through the controller, not as a behavior in the model which I need to get working so I can use the feature to pass the file into Amazon s3.

My code is as follows, can anyone see where I am going wrong? At the moment the database record is generated but only with the other fields I have on the form (caption, card_id, user_id) nothing to do with the file comes though. the file is not uploaded either.

Model: DataFile.php

public $actsAs = array(
    'Uploader.FileValidation' => array(
        'file1' => array(
            'required' => true
        ),
        'file2' => array(
            'required' => false
        ),
        'file3' => array(
            'required' => true
        )
    ),
    'Uploader.Attachment' => array(
        'file' => array(
            'name' => '',
            'uploadDir' => 'files/data_files/',
            'dbColumn' => 'path',
            'maxNameLength' => 30,
            'overwrite' => true,
            'stopSave' => false,
            's3'        => array(
                                'accessKey' => 'MYACCESSKEY',
                                'secretKey' => 'MYSECRETKEY',
                                'ssl' => true,
                                'bucket' => 'testfilespath',
                                'path' => '/'
                            ),                  // Array of Amazon S3 settings              
            'metaColumns' => array(
                    'ext' => 'extension',
                    'size' => 'bytesize',
                    'group' => 'group',
                    'width' => 'width',
                    'height' => 'height',
                    'filesize' => 'filesize'
            )
        )
    )
);  

Controller: DataFileController.php

// ADD BY BEHAVIOUR NEW FILE(S) - NOT WORKING
// ---------------------------------------------------------->
function add_behavior() 
{
    if (!empty($this->request->data)) 
    {

        if ($this->DataFile->save($this->request->data)) 
        {
            debug($this->request->data);

            $this->Session->setFlash(__('The File has been uploaded');
            $this->redirect(array('action' => 'index'));
        } 
        else 
        {
            $this->Session->setFlash(__('The DataFile could not be saved. Please, try again.'));
        }
    }
}

View: add_behavior.ctp

<?php echo $this->Form->create('DataFile', array('type' => 'file')); ?>
    <?php
    echo $this->Form->input('user_id', array('value' => $this->Session->read("Auth.User.id"),  'type' => 'text'));
    echo $this->Form->input('card_id', array('value' => '1',  'type' => 'text'));
    echo $this->Form->input('caption', array('label' => 'File Title'));
    echo $this->Form->input('file1', array('type' => 'file', 'label' => 'File'));
    ?>
    <?php echo $this->Form->end(__('Upload'));?>

Solution

  • Uploader.Attachment => array(
            'file1' => array(...),
            'file2' => array(...),
            'file3' => array(...),
    );
    

    When using the behaviour you must specify the name of your file field within the Uploader.Attachment array.

    Your form field is called file1, the behaviour is currently looking for file.