Cake PHP 5 - Object of class Laminas\Diactoros\UploadedFile could not be converted to string
stosef opened this issue · 5 comments
At first let me thank all authors of this plugin for their work!
I use cakephp-upload in a CakePHP 3 project and it works great.
Now I want to use it with CakePHP 5 and I can't get it to work.
Here is the config for the table:
class CustomersTable extends Table
{
/**
* Initialize method
*
* @param array<string, mixed> $config The configuration for the Table.
* @return void
*/
public function initialize(array $config): void
{
parent::initialize($config);
$this->setTable('customers');
$this->setDisplayField('name');
$this->setPrimaryKey('id');
$this->addBehavior('Josegonzalez/Upload.Upload', [
'customer_logo' => [
'path' => 'Files/logos/'
],
]);
}
}
This is my config/plugins.php:
<?php
return [
'DebugKit' => [
'onlyDebug' => true,
],
'Bake' => [
'onlyCli' => true,
'optional' => true,
],
'Migrations' => [
'onlyCli' => true,
],
'Josegonzalez/Upload' => [],
];
This is how I use it in my template:
<?php
echo $this->Form->create($customer, [
'type' => 'file'
]);
?>
<?= $this->Form->control('customer_logo', ['type' => 'file']); ?>
When I try to upload a file I get "Object of class Laminas\Diactoros\UploadedFile could not be converted to string"
Debugger shows that the value that gets written to the DB was not cast to a string. I think it should be a string at this point?
Does anyone have a working example with CakePHP 5? Am I missing some config or code?
Thank you
I guess it has something to do with a breaking change in CakePHP 5 regarding file uploads: https://book.cakephp.org/5/en/appendices/5-0-migration-guide.html#http
The plugin has been compatible with CakePHP 5.x since quite a while. Use one of CakePHP's help forum to figure out your issue.
You may need to ensure that the type for the column in your model class is being set to File?
https://github.com/FriendsOfCake/cakephp-upload/blob/master/src/Database/Type/FileType.php
You may need to ensure that the type for the column in your model class is being set to File?
https://github.com/FriendsOfCake/cakephp-upload/blob/master/src/Database/Type/FileType.php
Thank you, I have tried it with no success.
Do you have a code snippet how you did it?
I have found the issue. The folder permissions for the upload folder have not been set properly. After fixing permissions everything is working as expected now.