I am using the cakephp-upload plugin and I managed to upload images to my server:
WorkersTable:
public function initialize(array $config)
{
parent::initialize($config);
$this->table('workers');
$this->displayField('id');
$this->primaryKey('id');
$this->addBehavior('Josegonzalez/Upload.Upload', [
'avatar' => [
'fields' => [
'dir' => 'photo_dir'
]
]
]);
}
view.ctp:
echo $this->Form->create($newWorker, ['type' => 'file']);
echo $this->Form->input('avatar', ['type' => 'file']);
echo $this->Form->input('photo_dir', ['type' => 'hidden']);
Now the avatar images are uploaded, but they are not put into the photo_dir subdirectory.
What am I missing? It works without any problems in my CakePHP 2.8.x application.
Author of the plugin here.
The fields.dir
attribute does not state what the subdirectory should be. It is a reference to the column in your database table where we should save the director
where we saved the file.
If you want to change the place where you save files on disk, you should instead use the path
option. Here is an example where i use the photo_dir
subdirectory:
$this->addBehavior('Josegonzalez/Upload.Upload', [
'avatar' => [
'path' => 'webroot{DS}files{DS}{model}{DS}{field}{DS}photo_dir{DS}'
]
]);
The default value for the path
option is webroot{DS}files{DS}{model}{DS}{field}{DS}
.
Shouldn't it be:
$this->addBehavior('Josegonzalez/Upload.Upload', [
'avatar' => [
'fields' => [
'dir' => 'avatar_dir'
]
]
]);
echo $this->Form->input('avatar_dir', ['type' => 'hidden']);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With