【发布时间】:2012-05-07 20:29:37
【问题描述】:
我正在尝试在我的 AttachmentsController 中动态设置 $uploadDir,但每当我尝试使用时:
$this->Uploader = new Uploader( array('uploadDir' => "/files/uploads/clients/".$id."/" ) );
uploadDir 变量默认为 Plugin/Uploader/Vendor/Uploader.php 文件中的那个。
我尝试从 $actAs 数组和 AttachmentBehavior.php 中的 $_defaults 中删除“uploadDir”变量声明,但没有成功。
我只用了几个星期的 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 类中的文件夹。必须更改所有这些文件夹以找出哪个文件在控制事物。
我希望插件的文档有更多示例,并更清楚地说明如何进行初始设置。
【问题讨论】:
标签: cakephp plugins cakephp-2.0 file-upload uploader