CakePHP Uploader插件 - 行为设置

时间:2012-10-16 21:03:12

标签: cakephp amazon-s3 uploader

我正在使用这个插件:MileJ CakePHP Uploader它的效果非常好但是我只能让它通过控制器工作,而不是作为我需要工作的模型中的行为所以我可以使用该功能将文件传递到亚马逊s3。

我的代码如下,谁能看到我哪里错了?在生成数据库记录的那一刻,但只有我在表单上的其他字段(标题,card_id,user_id)与文件无关。该文件也未上传。

型号: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'
            )
        )
    )
);  

控制器: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.'));
        }
    }
}

查看: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'));?>

1 个答案:

答案 0 :(得分:2)

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

使用此行为时,您必须在Uploader.Attachment数组中指定文件字段的名称。

您的表单字段名为file1,该行为目前正在查找file