2012-04-27 56 views
1

我想在我的AttachmentsController動態設置$ uploadDir但每當我嘗試使用:

$this->Uploader = new Uploader(array('uploadDir' => "/files/uploads/clients/".$id."/")); 

的uploadDir變量默認爲一個在插件/上傳/供應商/ Uploader.php文件。

我試着從$ actAs數組中刪除'uploadDir'變量聲明,並從$ AttachmentBehavior.php中的$ _defaults,但沒有運氣。

我只使用CakePHP幾個星期,我似乎無法弄清楚如何使這項工作。

當我print_r($ this-> Uploader); uploadDir變量設置正確,但是當我檢查我的上傳目錄時,它會保存到Uploader.php類中設置的默認位置。

下面的代碼:

AttachmentsController.php

App::import('Vendor', 'Uploader.Uploader'); 

class AttachmentsController extends AppController {   

public $helpers = array("Html", "Form", "Session"); 
public $uses = array("Attachment"); 

public function index() 
{   
    $docs = $this->Attachment->findAllByClientIdAndUserId($this->Session->read("Client.id"), AuthComponent::user('id')); 
    $this->set("page",10); 
    $this->set("docs",$docs); 
} 

public function upload() 
{ 

    if($this->request->is('post')) 
    { 
     $id = $this->Session->read("Client.id"); 
     unset($this->Uploader); 
     $this->Uploader = new Uploader(array('uploadDir' => '/files/uploads/wizno/')); 
     $this->Uploader->setup(array('uploadDir' => '/files/uploads/wizno/')); 
     //print_r($this->Uploader);    
     //exit; 

      if(!empty($this->data)){ 
       if($this->Attachment->save($this->data)) 
       { 
        $this->redirect("/attachments"); 
       } 
      } 
    } 

} 

}

Attachment.php

class Attachment extends AppModel { 
public $name = "Attachment"; 
public $belongsTo = array("Client","User"); 
public $useTable = "uploads"; 

public $virtualFields = array(
'created' => 'DATE_FORMAT(Attachment.created, "%m/%d/%Y")', 
'modified' => 'DATE_FORMAT(Attachment.modified, "%m/%d/%Y")' 
); 

public $actsAs = array(  
    'Uploader.Attachment' => array(
     'file' => array(
      'name'  => '', 
      'uploadDir' => "/files/uploads/clients/WTF/500", 
      'dbColumn' => 'path', 
      'maxNameLength' => 50, 
      'overwrite' => true, 
      'stopSave' => true, 

      'metaColumns' => array(
       'size' => 'filesize', // The size value will be saved to the filesize column 
       'type' => 'type'  // And the same for the mimetype 
      ) 
        ) 
    ) 
); 

}

Uploader.php類 這個變量總是覆蓋我的設置,我不知道如何使它合作。

/** 
* Destination upload directory within $baseDir. 
* 
* @access public 
* @var string 
*/ 
public $uploadDir = 'files/uploads/2012/'; 

AttachmentBehavior.php

protected $_defaults = array(
    'name' => '', 
    'baseDir' => '', 
    'uploadDir' => '/files/100/',  
    'dbColumn' => 'path',  
    'defaultPath' => '', 
    'maxNameLength' => null, 
    'overwrite' => false,   // Overwrite a file with the same name if it exists 
    'stopSave' => true,    // Stop model save() on form upload error 
    'allowEmpty' => true,   // Allow an empty file upload to continue 
    'saveAsFilename' => true,  // If true, will only save the filename and not relative path    
    'metaColumns' => array(
     'ext' => '', 
     'type' => '', 
     'size' => '', 
     'group' => '', 
     'width' => '', 
     'height' => '', 
     'filesize' => '' 
    ) 
); 

儘管具有 'uploadDir' 設爲不同的文件夾中的各種文件,但它仍然默認爲一個在Uploader.php類。必須改變所有這些文件夾以找出哪個文件控制着事物。

我希望插件的文檔有更多的例子和如何做初始設置的更清晰的解釋。

回答

1

那麼,爲什麼不

CakePlugin::load('Uploader');  
App::import('Vendor', 'Uploader.Uploader'); 

$this->Uploader = new Uploader(array('tempDir' => TMP,'uploadDir'=> DS . 'custom_dir' . DS)); 

//OR 

$this->Uploader->uploadDir = DS . 'custom_dir' . DS; 

您可以隨時提交文件和錯誤,以Miles'github repos