nimrod
nimrod

Reputation: 5732

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.

Upvotes: 6

Views: 946

Answers (3)

Jose Diaz-Gonzalez
Jose Diaz-Gonzalez

Reputation: 2232

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

Upvotes: 1

Hardik Patel
Hardik Patel

Reputation: 716

If you want to use better option than you can use below plugin which has better option for upload the file rather than Josegonzalez/Upload.Upload plugin.

I have used 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 customise it. Let me know if you have any question regarding this.

Upvotes: 1

Gilko
Gilko

Reputation: 2387

Shouldn't it be:

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

echo $this->Form->input('avatar_dir', ['type' => 'hidden']);

Upvotes: 1

Related Questions