Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

enter image description here

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

like image 560
nimrod Avatar asked Jul 13 '16 20:07

nimrod


2 Answers

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}.

like image 91
Jose Diaz-Gonzalez Avatar answered Oct 02 '22 02:10

Jose Diaz-Gonzalez


Shouldn't it be:

$this->addBehavior('Josegonzalez/Upload.Upload', [
     'avatar' => [
         'fields' => [
             'dir' => 'avatar_dir'
         ]
     ]
 ]);

echo $this->Form->input('avatar_dir', ['type' => 'hidden']);
like image 35
Gilko Avatar answered Oct 02 '22 01:10

Gilko