У меня есть небольшой сценарий загрузки, и я следил за учебником, документами и аналогичной проблемой в этом посте Cake PHP: объект класса Laminas \ Diactoros \ UploadedFile не может быть преобразован в строку во время загрузки файла , но я получаю ту же ошибку. Загрузка работала нормально до того, как я добавил правила проверки загрузки.
Ошибка
Object of class Laminas\Diactoros\UploadedFile could not be converted to string
добавление стратегий. php
<?= $this->Form->control('strategies_screenshots.0.image_file', ['type' => 'file']); ?>
StrategiesController
public function add()
{
$strategy = $this->Strategies->newEmptyEntity();
if ($this->request->is('post')) {
$strategy = $this->Strategies->patchEntity($strategy, $this->request->getData());
if(!$strategy-getErrors) {
$image = $this->request->getData('strategies_screenshots.0.image_file');
$fileName = $image->getClientFilename();
$targetPath = WWW_ROOT.'screenshots'.DS.$fileName;
if($image_name)
$image->moveTo($targetPath);
$strategy['strategies_screenshots'][0]['image_name'] = $fileName;
}
if ($this->Strategies->save($strategy)) {
$this->Flash->success(__('The strategy has been saved.'));
return $this->redirect(['action' => 'index']);
}
$this->Flash->error(__('The strategy could not be saved. Please, try again.'));
}
$users = $this->Strategies->Users->find('list', ['limit' => 200]);
$this->set(compact('strategy', 'users'));
}
Стратегии Скриншоты Таблица
$validator
->notEmptyFile('image_file')
->uploadedFile('image_file', [
'types' => ['image/png'], // only PNG image files
'minSize' => 1024, // Min 1 KB
'maxSize' => 10240 * 1024 // Max 10 MB
])
->add('image_file', 'filename', [
'rule' => function (UploadedFileInterface $file) {
// filename must not be a path
$filename = $file->getClientFilename();
if (strcmp(basename($filename), $filename) === 0) {
return true;
}
return false;
}
])
->add('image_file', 'extension', [
'rule' => ['extension', ['png']] // .png file extension only
]);
Подробная ошибка:
[Error] Object of class Laminas\Diactoros\UploadedFile could not be converted to string in /Applications/MAMP/htdocs/trading-journal/vendor/cakephp/cakephp/src/Database/Type/StringType.php on line 97
Stack Trace: - /Applications/MAMP/htdocs/trading-journal/vendor/cakephp/cakephp/src/ORM/Marshaller.php:78
- /Applications/MAMP/htdocs/trading-journal/vendor/cakephp/cakephp/src/ORM/Marshaller.php:210
- /Applications/MAMP/htdocs/trading-journal/vendor/cakephp/cakephp/src/ORM/Marshaller.php:363
- /Applications/MAMP/htdocs/trading-journal/vendor/cakephp/cakephp/src/ORM/Marshaller.php:332
- /Applications/MAMP/htdocs/trading-journal/vendor/cakephp/cakephp/src/ORM/Marshaller.php:735
- /Applications/MAMP/htdocs/trading-journal/vendor/cakephp/cakephp/src/ORM/Marshaller.php:115
- /Applications/MAMP/htdocs/trading-journal/vendor/cakephp/cakephp/src/ORM/Marshaller.php:566
- /Applications/MAMP/htdocs/trading-journal/vendor/cakephp/cakephp/src/ORM/Table.php:2795
- /Applications/MAMP/htdocs/trading-journal/src/Controller/StrategiesController.php:54
- /Applications/MAMP/htdocs/trading-journal/vendor/cakephp/cakephp/src/Controller/Controller.php:521
- /Applications/MAMP/htdocs/trading-journal/vendor/cakephp/cakephp/src/Controller/ControllerFactory.php:79
- /Applications/MAMP/htdocs/trading-journal/vendor/cakephp/cakephp/src/Http/BaseApplication.php:229
- /Applications/MAMP/htdocs/trading-journal/vendor/cakephp/cakephp/src/Http/Runner.php:77
- /Applications/MAMP/htdocs/trading-journal/vendor/cakephp/cakephp/src/Http/Middleware/BodyParserMiddleware.php:164
- /Applications/MAMP/htdocs/trading-journal/vendor/cakephp/cakephp/src/Http/Runner.php:73
- /Applications/MAMP/htdocs/trading-journal/vendor/cakephp/cakephp/src/Http/Runner.php:77
- /Applications/MAMP/htdocs/trading-journal/vendor/cakephp/cakephp/src/Http/Middleware/CsrfProtectionMiddleware.php:138
- /Applications/MAMP/htdocs/trading-journal/vendor/cakephp/cakephp/src/Http/Runner.php:73
- /Applications/MAMP/htdocs/trading-journal/vendor/cakephp/cakephp/src/Http/Runner.php:58
- /Applications/MAMP/htdocs/trading-journal/vendor/cakephp/cakephp/src/Routing/Middleware/RoutingMiddleware.php:166
- /Applications/MAMP/htdocs/trading-journal/vendor/cakephp/cakephp/src/Http/Runner.php:73
- /Applications/MAMP/htdocs/trading-journal/vendor/cakephp/cakephp/src/Routing/Middleware/AssetMiddleware.php:68
- /Applications/MAMP/htdocs/trading-journal/vendor/cakephp/cakephp/src/Http/Runner.php:73
- /Applications/MAMP/htdocs/trading-journal/vendor/cakephp/cakephp/src/Error/Middleware/ErrorHandlerMiddleware.php:119
- /Applications/MAMP/htdocs/trading-journal/vendor/cakephp/cakephp/src/Http/Runner.php:73
- /Applications/MAMP/htdocs/trading-journal/vendor/cakephp/debug_kit/src/Middleware/DebugKitMiddleware.php:60
- /Applications/MAMP/htdocs/trading-journal/vendor/cakephp/cakephp/src/Http/Runner.php:73
- /Applications/MAMP/htdocs/trading-journal/vendor/cakephp/cakephp/src/Http/Runner.php:58
- /Applications/MAMP/htdocs/trading-journal/vendor/cakephp/cakephp/src/Http/Server.php:90
- /Applications/MAMP/htdocs/trading-journal/webroot/index.php:40
Request URL: /strategies/add Referer URL: http://localhost:8888/trading-journal/strategies/add
Вывод debug($this->request->getData());
/src/Controller/StrategiesController.php (line 54)
[
'user_id' => '1',
'name' => 'sdf',
'description' => '',
'strategies_screenshots' => [
(int) 0 => [
'image_file' => object(Laminas\Diactoros\UploadedFile) {
[private] clientFilename => 'Screen Shot 2020-04-15 at 10.11.59.png'
[private] clientMediaType => 'image/png'
[private] error => (int) 0
[private] file => '/Applications/MAMP/tmp/php/phpbwWNVX'
[private] moved => false
[private] size => (int) 348653
[private] stream => null
}
]
]
]
Я также пробовал:
public function validationDefault(Validator $validator): Validator
{
$validator
->integer('id')
->allowEmptyString('id', null, 'create');
$validator
->allowEmptyFile('image_name')
->add( 'image_name', [
'mimeType' => [
'rule' => [ 'mimeType', [ 'image/jpg', 'image/png', 'image/jpeg' ] ],
'message' => 'Please upload only jpg and png.',
],
'fileSize' => [
'rule' => [ 'fileSize', '<=', '10MB' ],
'message' => 'Image file size must be less than 1MB.',
],
] );
Когда я запускаю следующее, у меня нет проблем:
$validator
->scalar('image_name')
->maxLength('image_name', 100)
->requirePresence('image_name', 'create')
->notEmptyFile('image_name');