[plugin cakephp-upload] Upload_err

Hi guys, how are you?
First, sorry for my English, I’m Brazilian and I’m learning English and CakePHP.
So, i have an application and i need upload multiple photos.
For this i use this plugin: GitHub - fm-labs/cakephp-upload: CakePHP Upload Plugin
When uploading the photo, it gives me this error:
image

if put dd( $this->getRequest()->getData(‘uploadfile’)); i recive this message:

image

so i can’t understand what’s going on

this is my controller

        if ($this->request->is('post')) {
            $uploader = new \Upload\Uploader([]);
            $uploader->upload($this->getRequest()->getData('uploadfile'));
            $result = $uploader->getResult();
        }

and my view:

<?= $this->Form->create(null); ?>
<?= $this->Form->input('uploadfile', ['type' => 'file']); ?>
<?= $this->Form->submit(); ?>
<?= $this->Form->end(); ?>

Instead of using that plugin I would rather recommend you use the default CakePHP file parsing functionality which is mentioned here:
https://book.cakephp.org/4/en/controllers/request-response.html#file-uploads

and here

Since you are uploading a file the form needs to know this as well, so you need

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

instead of

<?= $this->Form->create(null); ?>

This will then result in a \Laminas\Diactoros\UploadedFile file being present inside your request data, so like

/** @var \Laminas\Diactoros\UploadedFile $file */
$file = $this->request->getData('uploadfile');

which then has all the necessary data inside it (see first link above)

My guess would be, that the plugin creator built this with the “old” array behavior mentioned in the first link above which can be set inside your config:

'App' => [
    // ...
    'uploadedFilesAsObjects' => false,
],

but I could be wrong there.

1 Like

ok, I’m trying your way, it happens that I can save the images, however in the bank it saves only one.
this is my controller:

My view:

My DB:

my local save:
image

$photo inside your foreach is still the same entity you created at the top with ->newEmptyEntity()
So it always has the same ID.

You are writing the first image name to your entity, saving it, then overwriting the name again with the second image and saving again.

If you want separate entries inside your table you need to create separate entities for each uploaded image.

1 Like