How to solve the CakePHP warning message "Object of class Laminas\Diactoros\UploadedFile could not be converted to string"?

526 Views Asked by At

The submission will be added to the database and the images will be uploaded to the img and img2 folders. However, if the submission is successful, I still get the warning message "Object of class Laminas\Diactoros\UploadedFile could not be converted to string". I am stuck on how to hide the warning message or solve the warning message. I have image validation and I am not sure why that doesn't resolve the warning message. Any suggestions or help would be greatly appreciated. A similar Stack Overflow question helped me make progress on this problem, but I still haven't found a solid solution.

The controller file is how the images are uploaded to the img and img2 folders. It is how the submission is posted to the database. (SubmissionsController.php)

$submission = $this->Submissions->newEmptyEntity();
            if ($this->request->is('post')) {

                $submission = $this->Submissions->patchEntity($submission, $this->request->getData());

                if(!$submission->getErrors()) {
                    $image  = $this->request->getData('image_path');
                    $name   = $image->getClientFilename();
                    $image->moveTo(WWW_ROOT . 'img' . DS . $name);
                    $submission->image_path = $name;

                    $image2 = $this->request->getData('image_path2');
                    $name2  = $image2->getClientFilename();
                    $image2->moveTo(WWW_ROOT . 'img2' . DS . $name2);
                    $submission->image_path2 = $name2;
                }

                if($this->Submissions->save($submission)) {
                    if($this->Auth->user('UserGroupID') == 3 || $this->Auth->user('UserGroupID') == 2) {
                        $this->Flash->success(__('The submission has been saved.'));
                        return $this->redirect(['action' => 'index']);
                    } else {
                        $this->Flash->success(__('The submission has been saved.'));
                        return $this->redirect(array('controller' => 'ModelTypes', 'action' => 'index'));
                    }
                } else {
                    $this->Flash->error(__('The submission could not be saved. Make sure that the file is an image with these file extensions (jpg, jpeg, png).'));
                }
            }

The table file is where the image field validation takes place. I think this is where code needs to go to solve the warning message. (SubmissionsTable.php)

       $validator
            ->notEmptyFile('image_path')
            ->uploadedFile('image_path', [
                'types' => ['image/jpg', 'image/png', 'image/jpeg'],
                'minSize' => 1024, // Min 1 KB
                'maxSize' => 1024 * 1024 // Max 1 MB
            ])
            ->add('image_path', [
                'mimeType' => [
                    'rule' => [ 'mimeType', [ 'image/jpg', 'image/png', 'image/jpeg' ] ],
                    'message' => 'Please upload only jpg, jpeg, and png.',
                ],
                'fileSize' => [
                    'rule' => [ 'fileSize', '<=', '1MB' ],
                    'message' => 'Image file size must be less than 1MB.',
                ]
            ])
            ->add('image_path', '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_path', 'extension', [
                'rule' => ['extension', ['png', 'jpg', 'jpeg']]
            ]);
       $validator
            ->notEmptyFile('image_path2')
            ->uploadedFile('image_path2', [
                'types' => ['image/jpg', 'image/png', 'image/jpeg'],
                'minSize' => 1024, // Min 1 KB
                'maxSize' => 1024 * 1024 // Max 1 MB
            ])
            ->add('image_path2', [
                'mimeType' => [
                    'rule' => [ 'mimeType', [ 'image/jpg', 'image/png', 'image/jpeg' ] ],
                    'message' => 'Please upload only jpg, jpeg, and png.',
                ],
                'fileSize' => [
                    'rule' => [ 'fileSize', '<=', '1MB' ],
                    'message' => 'Image file size must be less than 1MB.',
                ]
            ])
            ->add('image_path2', '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_path2', 'extension', [
                'rule' => ['extension', ['png', 'jpg', 'jpeg']]
            ]);

These are the two image form fields. (add.php)

<div class="row">
        <div class="col-12 col-sm-6 mt-3">
            <?php
                echo $this->Form->control('image_path', array('type' => 'file'));
            ?>
        </div>
        <div class="col-12 col-sm-6 mt-3">
            <?php
                echo $this->Form->control('image_path2', array('type' => 'file'));
            ?>
        </div>
    </div>

Error Message:

Warning (4096): Object of class Laminas\Diactoros\UploadedFile could not be converted to string [CORE\src\Database\Type\StringType.php, line 97]
Cake\Database\Type\StringType::marshal() - CORE\src\Database\Type\StringType.php, line 97
Cake\ORM\Marshaller::Cake\ORM\{closure}() - CORE\src\ORM\Marshaller.php, line 78
Cake\ORM\Marshaller::merge() - CORE\src\ORM\Marshaller.php, line 566
Cake\ORM\Table::patchEntity() - CORE\src\ORM\Table.php, line 2795
App\Controller\SubmissionsController::add() - APP/Controller\SubmissionsController.php, line 37
Cake\Controller\Controller::invokeAction() - CORE\src\Controller\Controller.php, line 521
Cake\Controller\ControllerFactory::invoke() - CORE\src\Controller\ControllerFactory.php, line 79
Cake\Http\BaseApplication::handle() - CORE\src\Http\BaseApplication.php, line 229
Cake\Http\Runner::handle() - CORE\src\Http\Runner.php, line 77
Cake\Http\Middleware\BodyParserMiddleware::process() - CORE\src\Http\Middleware\BodyParserMiddleware.php, line 164
Cake\Http\Runner::handle() - CORE\src\Http\Runner.php, line 73
Cake\Http\Runner::handle() - CORE\src\Http\Runner.php, line 77
Cake\Http\Middleware\CsrfProtectionMiddleware::process() - CORE\src\Http\Middleware\CsrfProtectionMiddleware.php, line 138
Cake\Http\Runner::handle() - CORE\src\Http\Runner.php, line 73
Cake\Http\Runner::run() - CORE\src\Http\Runner.php, line 58
Cake\Routing\Middleware\RoutingMiddleware::process() - CORE\src\Routing\Middleware\RoutingMiddleware.php, line 166

It looks like the error is being thrown on this line:

$submission = $this->Submissions->patchEntity($submission, $this->request->getData());
0

There are 0 best solutions below