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

3

6

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.

Booth answered 13/7, 2016 at 20:11 Comment(2)
does the photo_dir actually exist on the server?Electroform
photo_dir is a column in the workers table.Booth
C
1

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

Chieftain answered 22/7, 2016 at 20:46 Comment(0)
W
1

Shouldn't it be:

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

echo $this->Form->input('avatar_dir', ['type' => 'hidden']);
Wet answered 13/7, 2016 at 22:22 Comment(0)
S
1

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.

Salpinx answered 21/7, 2016 at 9:9 Comment(1)
Did you try this solution ?Salpinx
C
1

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

Chieftain answered 22/7, 2016 at 20:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.