FriendsOfCake / cakephp-upload

CakePHP: Handle file uploading sans ridiculous automagic
https://cakephp-upload.readthedocs.io/
MIT License
551 stars 255 forks source link

How to upload files inside a folder named with galler_id #489

Closed karthikeyanangler closed 6 years ago

karthikeyanangler commented 6 years ago

Dear Friends,

I am trying to use this plugin for gallery images module in my portal. I am trying to store the images in following directory webroot/files/galleries/<gallery_id>. How can i achieve it? i was trying to add this as hidden value in the form like this

echo $this->Form->input('file_dir', array('type' => 'hidden', 'value' => 'webroot/files/galleries/'.$gallery_id));

But its not helping me to resolve my requirement, please help me.

My Behavior Configuration in GalleryImagesTable.php file

# Add Behaviour for file Upload
$this->addBehavior('Josegonzalez/Upload.Upload', [
    'file' => [
        'path' => 'webroot{DS}files{DS}galleries{DS}',
        'fields' => [
            // if these fields or their defaults exist
            // the values will be set.
            'dir' => 'file_dir', // defaults to `dir`
            'size' => 'file_size', // defaults to `size`
            'type' => 'file_type', // defaults to `type`
        ],
    ]
]);

Gallery Image add.ctp file

<?= $this->Form->create($galleryImages, ['type' => 'file']) ?>
<fieldset>
    <?php /*<legend><?= __('Add Gallery Image') ?></legend> */ ?>
    <?php
        echo $this->Form->control('gallery_id', ['class' => 'form-control', 'options' => $galleries]);
        echo $this->Form->control('file', ['class' => 'form-control', 'type' => 'file']);
        echo $this->Form->input('file_dir', array('type' => 'hidden', 'value' => 'webroot/files/galleries/'.$gallery_id));
    ?>
</fieldset>
<br />
<?= $this->Form->button(__('Submit'), ['class' => 'btn btn-lg btn-success']) ?>
<?= $this->Form->end() ?>
davidyell commented 6 years ago

I would recommend implementing your own Path Processor class for this job. As then you can write your own code to build the path using whatever data you choose.

http://cakephp-upload.readthedocs.io/en/latest/interfaces.html#processorinterface

raul338 commented 6 years ago

I use the field value, in the table

$this->addBehavior('Josegonzalez/Upload.Upload', [
    'nombre' => [
        'path' => 'webroot{DS}img{DS}uploads{DS}tickets{DS}{field-value:ticket_id}{DS}',
    ],
]);
karthikeyanangler commented 6 years ago

@raul338 Amazing, its working, thanks a lot 👍