0

首先,感谢 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;
    }

我想为我的代码避免这种方式,任何人都可以提供解决方案吗?对不起,我的英语不好

4

1 回答 1

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

于 2013-10-30T11:21:34.377 回答