2013-04-30 71 views
0

我目前正在cakephp 2.3中運行php5環境。Cakephp FileUpload插件 - 會話數據 - 自定義目錄路徑

我設法下載並實現了基於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; 
     } 
    } 

} 

現在我需要改變upload_dir追加基於在用戶ID登錄。 是這樣的:在UploadComponent,但沒有運氣

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

我曾嘗試聲明變量$組件=陣列(「會議」)。

我也積極地認爲創建目錄不工作,因爲如果我對upload_dir進行硬編碼,它不會生成文件。

我是cakephp的初學者,所以明顯的步驟可能已經錯過了。

問候,

Cloud_R

回答

1

雨果設法給我發電子郵件支持:

$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').'/', 
     ... 

這會自動解決了這個問題。