FriendsOfCake / cakephp-upload

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

Multiple Uploads with one input, I just don't get it. #501

Closed benoitkopp closed 2 years ago

benoitkopp commented 5 years ago

Hello, I used the plugin to upload some single files and it worked perfectly. I'm trying to upload multiple files in one time, but I'm not able to make it work.

My Parent Table MeetingsStatements:

`id` int(11) NOT NULL AUTO_INCREMENT,
  `meeting_id` int(11) DEFAULT NULL,
  `user_id` varchar(255) COLLATE utf8mb4_bin DEFAULT NULL,
  `date_statement` date NOT NULL,
  `data` mediumtext COLLATE utf8mb4_bin NOT NULL,
  `type` mediumtext COLLATE utf8mb4_bin NOT NULL,
  `created` datetime NOT NULL,
  `modified` datetime NOT NULL,
  `visibility` varchar(255) COLLATE utf8mb4_bin NOT NULL,
  `pinned` tinyint(1) NOT NULL DEFAULT '0',

My Child Table MeetingsStatementFiles:

`id` int(11) NOT NULL AUTO_INCREMENT,
  `meeting_statement_id` int(11) NOT NULL,
  `name` varchar(255) NOT NULL,
  `type` varchar(255) NOT NULL,
  `tmp_name` varchar(255) NOT NULL,
  `size` int(11) NOT NULL,

In my CakePHP MeetingsStatementsTable:


$this->hasMany('MeetingsStatementFiles', [
            'foreignKey' => 'meeting_statement_id',
            'saveStrategy' => 'replace',
            'dependent' => true
        ]);

In my CakePHP MeetingsStatementFilesTable:

$this->addBehavior('Josegonzalez/Upload.Upload', [
            'meetings_statement_files' => [
                'dir' => 'attachements',
                'nameCallback' => function ($data, $settings) {
                    return time() . '-' . $data['name'];
                },
                'keepFilesOnDelete' => false,
            ]
        ]);

        $this->belongsTo('MeetingsStatements', [
            'foreignKey' => 'meeting_statement_id',
            'joinType' => 'INNER'
        ]);

I wrote my input like: <input type="file" name="meetings_statement_files[]" multipe >

My relations are pretty good set, I can save and delete the MeetingsStatementFiles entries depending on the MeetingsStatements but the uploads are still not working.

What am I doing wrong ?

Thanks.

davidyell commented 5 years ago

Is your form create a file type to generate a multi-part form?

$this->Form->create($entity, ['type' => 'file']);?

benoitkopp commented 5 years ago

Yes :)

berarma commented 5 years ago

The plugin configuration seems to have some errors.

You're configuring the field meetings_statement_files but that field doesn't exist in the model. Maybe you want to use name instead.

Also, you might intend to use path instead of dir.

tgoeminne commented 3 years ago

I have found that when you add multiple files like that you should put the following in controller, not sure if this was intended way to do it:

`$data = $this->request->getData();

if(isset($data['article_attachments'])){ $attachments = $data['article_attachments']; unset($data['article_attachments']); foreach($attachments as $attachment){ $data['article_attachments'][]['attachment'] = $attachment; } }

$article = $this->Articles->patchEntity($article, $data,['associated' => ['ArticleAttachments']]);`