【问题标题】:Cakephp FileUpload Plugin - Session Data - Custom Directory PathCakephp FileUpload 插件 - 会话数据 - 自定义目录路径
【发布时间】:2013-04-30 09:35:46
【问题描述】:

我目前正在开发在 php5 环境中运行的 cakephp 2.3。

我已经成功下载并实现了基于 blueimp 原始设计 (https://github.com/blueimp/jQuery-File-Upload) 的 Jquery 文件上传 (https://github.com/hugodias/FileUpload)。

似乎一切正常。但是我需要根据登录的用户详细信息动态更改上传目录。上传组件如下:

class UploadComponent extends Component
{   
protected $options;

/*
* $options = array()
* Avaliable Options:
*
* 
* $options => array(
*   'upload_dir' => 'files/{your-new-upload-dir}' // Default 'files/'
* )
*/
function __construct( ComponentCollection $collection, $options = null ) {

    $this->UploadModel = ClassRegistry::init('FileUpload.Upload');

    $this->options = array(
        'script_url' => Router::url('/', true).'file_upload/handler',
        'upload_dir' => WWW_ROOT.'files/',
        'upload_url' => $this->getFullUrl().'/files/',
        'param_name' => 'files',
        // Set the following option to 'POST', if your server does not support
        // DELETE requests. This is a parameter sent to the client:
        'delete_type' => 'DELETE',
        // The php.ini settings upload_max_filesize and post_max_size
        // take precedence over the following max_file_size setting:
        'max_file_size' => null,
        'min_file_size' => 1,
        'accept_file_types' => '/.+$/i', // For only accept images use this: ([^\s]+(\.(?i)(jpg|png|gif|bmp))$)
        'max_number_of_files' => null,
        // Set the following option to false to enable resumable uploads:
        'discard_aborted_uploads' => true,
        // Set to true to rotate images based on EXIF meta data, if available:
        'orient_image' => false,
        'image_versions' => array()
    );

    # Check if exists new options
    if( $options )
    {
        # Change the upload dir. If it doesn't exists, create it.
        if( $options['upload_dir'] )
        {

            // Remove the first `/` if it exists.
            if( $options['upload_dir'][0] == '/' )
            {
                $options['upload_dir'] = substr($options['upload_dir'], 1);
            }


            $dir = WWW_ROOT.$options['upload_dir'];

            // Create the directory if doesn't exists.
            if( !file_exists( $dir) )
            {
                @mkdir( $dir );
            } 

            $this->options['upload_url'] = $this->getFullUrl().'/'.$dir;
            $this->options['upload_dir'] = $dir;
        }
    }

}

现在我需要根据登录的用户 ID 更改 upload_dir 以附加。 比如:

'upload_dir' => WWW_ROOT.'files/'.$this->Session->read('Auth.User.id').'/',

我尝试在 UploadComponent 中声明 var $components = array('Session') 但没有成功。

我也很肯定创建目录不起作用,因为如果我对 upload_dir 进行硬编码,它不会生成文件。

我是 cakephp 的初学者,所以可能错过了明显的步骤。

问候,

Cloud_R

【问题讨论】:

    标签: php jquery cakephp jquery-plugins file-upload


    【解决方案1】:

    Hugo 设法给我发电子邮件支持:

    $this->options = array(
            'script_url' => Router::url('/', true).'file_upload/handler',
            'upload_dir' => WWW_ROOT.'files/'.AuthComponent::user('id').'/',
            'upload_url' => $this->getFullUrl().'/files/'.AuthComponent::user('id').'/',
            ...
    

    这自动解决了问题。

    【讨论】:

      猜你喜欢
      • 2019-10-02
      • 1970-01-01
      • 2014-12-29
      • 1970-01-01
      • 1970-01-01
      • 2014-12-30
      • 2017-10-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多