我正在尝试在AttachmentsController中动态设置$ uploadDir,但每当我尝试使用时:
$this->Uploader = new Uploader( array('uploadDir' => "/files/uploads/clients/".$id."/" ) );
uploadDir变量默认为Plugin / Uploader / Vendor / 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类中的文件夹。必须更改所有这些文件夹以找出控制事物的文件。
我希望插件的文档有更多示例,并更清楚地解释如何进行初始设置。
答案 0 :(得分: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
提交文档和错误