Fixing Laminas\Diactoros\UploadedFile Issues in CakePHP4

Fixing issues related to Laminas\Diactoros\UploadedFile.

When you get errors associated to the Laminas\Diactoros\UploadeFile, including inablility to reference protected variables, or object-usage issues, this may help resolve your issues.

The issue seems to be, in CakePHP4, that file uploads are processed as objects.

To resolve this, and return to standard file uploads in CakePHP4, edit your /config/app.php, and add the following:

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

 

Prior to doing that, in my controller, posting a file, and debugging it, my debug would return this, and all variables were protected & inaccessible:

APP/Controller\Admin\MyController.php
debug($this->request->getData('main_image'));

object(Laminas\Diactoros\UploadedFile) 
id:0 {
private clientFilename => 'my-uploaded-file.gif'
private clientMediaType => 'image/gif'
private error => (int) 0
private file => 'C:\WAMP\tmp\phpA456.tmp'
private moved => false
private size => (int) 201
private stream => null
}

 

After setting the 'uploadedFilesAsObjects' => false, here's my debug - nice, clean and accessible:

debug($this->request->getData('main_image'));

[
'tmp_name' => 'C:\WAMP\tmp\phpA1B.tmp',
'error' => (int) 0,
'name' => 'my-uploaded-file.gif',
'type' => 'image/gif',
'size' => (int) 201,
]

 

Here's a brief overview of creating a form with file upload in CakePHP4: https://codestop.com/articles/view/8

 

For additional resources, we used the following:

CakePHP Discourse Link

 

Share this Post