Cakephp,beforeUpload()函数使用里程数johnson Uploader进行覆盖

时间:2013-10-30 11:15:38

标签: php cakephp file-upload plugins

首先,感谢Miles Johnson为cakephp上传一个很棒的插件 但我有一些问题 我想在我的模型中使用beforeupload()函数设置uploaddir 但它没有在我的附件中添加数组的数据 它覆盖我所有的附件上传器配置 这是我的模特

class File extends AppModel {
    public $actsAs = array(
        'Uploader.Attachment' => array(
                'fileupload' => array(
                     'maxNameLength'    => 255,
                     'overwrite'    => true,
                     'stopSave'    => true,
                     'allowEmpty'    => false,
                     'dbColumn' => 'path_file',
                     'prepend' => '',
                     'uploadDir' => '',
                     'finalPath' =>''
            )
        )
    );

    public function beforeUpload()
    {
    $folderid = $this->data['File']['folder_id'];
    foreach($this->folder_path($folderid) as $row)
    {
        $folder = trim($row[0]['folder']);
        $subfolder = trim($row[0]['subfolder']);
    }
$options['prepend'] = Configure::read('dept_sess') . Configure::read('timeunique')."==";
$options['uploadDir'] = 'files/' . Configure::read('dept_sess') . '/' .$folder. '/' .$subfolder. "/";
        return $options;
        }
    }

假设我的控制器和$ this-> folder_path是对的, 当我在我的控制器上执行save()时 这是错误的 $ attachment

的调试结果
array(
    'prepend' => '0000010820131030174740==',
    'uploadDir' => 'files/00000108/folder/subfolder/'
)

我的$ attachment配置被覆盖了,并没有添加附件数组的数据。 为什么我的beforeUpload()函数无法在附件数组中添加数据? 对于最糟糕的解决方案,我只需在beforeupload()上添加所有附件数据,如:

public $actsAs = array(
        'Uploader.Attachment' => array(
                'fileupload' => array()));

    public function beforeUpload()
    {
    $folderid = $this->data['File']['folder_id'];
    foreach($this->folder_path($folderid) as $row)
    {
        $folder = trim($row[0]['folder']);
        $subfolder = trim($row[0]['subfolder']);
    }
    $options['name'] = Configure::read('dept_sess') . Configure::read('timeunique')."==";
    $options['baseDir'] = 'files/' . Configure::read('dept_sess') . '/' .$folder. '/' .$subfolder. "/";
    $options['uploadDir'] = 'files/' . Configure::read('dept_sess') . '/' .$folder. '/' .$subfolder. "/";
    $options['append'] = '';
    $options['overwrite '] = false;
    $options['prepend'] = Configure::read('dept_sess') . Configure::read('timeunique')."==";
    $options['dbColumn'] = 'file_path';
    $options['importFrom'] = '';
    $options['defaultPath'] = '';
    $options['maxNameLength'] = '';
    $options['stopSave'] = '';
    $options['allowEmpty'] = false;
    $options['saveAsFilename'] = '';
    $options['transforms'] = '';
    $options['s3']['host'] = '*****';
    $options['s3']['format'] = 'http://{host}/{bucket}/{path}';
    $options['s3']['accessKey'] = '';
    $options['s3']['secretKey'] = '';
    $options['s3']['ssl'] = '';
    $options['s3']['bucket'] = '';
    $options['s3']['path'] = '';
    $options['metaColumns']['ext'] = '';
    $options['metaColumns']['type'] = '';
    $options['metaColumns']['size'] = '';
    $options['metaColumns']['group'] = '';
    $options['metaColumns']['width'] = '';
    $options['metaColumns']['height'] = '';
    $options['metaColumns']['filesize'] = '';
    $options['finalPath'] = '';
    $options['field'] = '';
    return $options;
    }

我想为我的代码避免这种方式,任何人都可以给出解决方案吗? 抱歉我的英文不好

1 个答案:

答案 0 :(得分:1)

您需要在$options中接收beforeUpload(),然后便可以对其进行修改。由于你没有这样做,你只需要创建一个只包含你在函数中设置的选项的新数组。

public function beforeUpload($options)
{
    $folderid = $this->data['File']['folder_id'];
    foreach($this->folder_path($folderid) as $row) {
        $folder = trim($row[0]['folder']);
        $subfolder = trim($row[0]['subfolder']);
    }
    $options['prepend'] = Configure::read('dept_sess') . Configure::read('timeunique')."==";
    $options['uploadDir'] = 'files/' . Configure::read('dept_sess') . '/' .$folder. '/' .$subfolder. "/";
    return $options;
}

您可以看到$options传递给https://github.com/milesj/uploader/blob/master/Model/Behavior/AttachmentBehavior.php#L516