我目前正在开发在php5环境中运行的cakephp 2.3。
我已设法下载并实施基于blueimp原始设计(https://github.com/hugodias/FileUpload)的Jquery文件上传(https://github.com/blueimp/jQuery-File-Upload)。
一切似乎都正常。但是,我需要根据登录的用户详细信息动态更改上载目录。上传组件如下:
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
答案 0 :(得分: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').'/',
...
这自动解决了问题。