我目前正在开发在 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 的初学者,所以可能错过了明显的步骤。
问候,
云_R