I'm trying to set the $uploadDir dynamically in my AttachmentsController but whenever I try to use:
$this->Uploader = new Uploader( array('uploadDir' => "/files/uploads/clients/".$id."/" ) );
the uploadDir variable defaults to the one in the Plugin/Uploader/Vendor/Uploader.php file.
I've tried removing the 'uploadDir' variable declaration from the $actAs arrays, and from the $_defaults in AttachmentBehavior.php but no luck.
I've only been using CakePHP for a couple weeks, and I can't seem to figure out how to make this work.
When I print_r($this->Uploader); the uploadDir variable is set properly, but when I check my upload directory, it saves to the default location that is set in the Uploader.php class.
Code Below:
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 Class This variable always overrides my settings, and I don't know how to make it cooperate.
/**
* 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' => ''
)
);
Despite having 'uploadDir' set to different folders in the various files, it still defaults to the one in the Uploader.php Class. Had to change all these folders to find out which file was controlling things.
I wish the plugin's documentation had more examples and a clearer explanation of how to do the initial setup.