Images are not saved in photo_dir folder using cakephp-upload plugin on CakePHP 3.2

I use the cakephp-upload plugin and I was able 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 avatar images are uploaded, but they are not placed in the photo_dir subdirectory.

enter image description here

What am I missing? It works without any problems in my CakePHP 2.8.x application.

+6
source share
3 answers

The author of the plugin is here.

The fields.dir attribute fields.dir not indicate what the subdirectory should be. This is a link to a column in your database table where we have to save director , where we saved the file.

If you want to change the location where files are saved on disk, you should use the path option instead. Here is an example when 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 parameter is webroot{DS}files{DS}{model}{DS}{field}{DS} .

+1
source

Must not be:

 $this->addBehavior('Josegonzalez/Upload.Upload', [ 'avatar' => [ 'fields' => [ 'dir' => 'avatar_dir' ] ] ]); echo $this->Form->input('avatar_dir', ['type' => 'hidden']); 
+1
source

If you want to use a better option than using the plugin below, which has a better option for uploading a file rather than the Josegonzalez/Upload.Upload .

I used the below one in my project.

 Utils Plugin for Cake 3.x 

Link for this plugin: https://github.com/cakemanager/cakephp-utils

Documentation: http://cakemanager.org/docs/utils/1.0/behaviors/uploadable/

And this is the configuration:

 $this->addBehavior('Utils.Uploadable', [ 'image' => [ 'path' => '{ROOT}{DS}{WEBROOT}{DS}uploads{DS}{field}{DS}', 'fileName' => md5(rand(1000, 5000000)) . '.{extension}', 'removeFileOnDelete' => true, 'removeFileOnUpdate' => FALSE ], ]); 

Here you can configure it. Let me know if you have any questions regarding this.

+1
source

All Articles