Uploading Two pictures in one Form Cake PHP VERSION (4.x)

Hi. I hope that anyone can help me.
I have table pages that contain tow fileds images and cover.
I need to upload two pictures in one form, I see that in this version it is a bit difficult to deal with this error .

Object of class Laminas\Diactoros\UploadedFile could not be converted to a string

Question : how i can solve this problem and how i can allow images or cover to be empty

the table pages contain two fields (image and cover).
inside the PagesTable.php I added the following code.

$validator

        ->allowEmptyFile('image_file')

        ->add('image_file', [

            'mimeType' => [

                'rule' => ['mimeType', ['image/jpg', 'image/png', 'image/jpeg']],

                'message' => 'Please upload only jpg and png.',

            ],

            'fileSize' => [

                'rule' => ['fileSize', '<=', '1MB'],

                'message' => 'Image file size must be less than 1MB.',

            ],

        ]);

    $validator

        ->allowEmptyFile('cover')

        ->add('cover', [

            'mimeType' => [

                'rule' => ['mimeType', ['image/jpg', 'image/png', 'image/jpeg']],

                'message' => 'Please upload only jpg and png.',

            ],

            'fileSize' => [

                'rule' => ['fileSize', '<=', '1MB'],

                'message' => 'Image file size must be less than 1MB.',

            ],

        ]);

    return $validator;

Inside the view I addd the following Code:

<?= $this->Form->control( 'image_file', array( 'type' => 'file', 'label' => array( 'text' => __d('admin', 'Main Image'), 'class' => 'col-sm-2 control-label' ), 'class' => '', 'style' => 'width:83.3333%;margin-bottom:10px', 'placeholder' => __d('admin', 'Main Image'), 'required' ) ) ?>
<?= $this->Form->control(

  'cover',

  array(

    'type' => 'file',

    'label' => array(

      'text' => __d('admin', 'Cover'),

      'class' => 'col-sm-2 control-label'

    ),

    'class' => '',

    'style' => 'width:83.3333%;margin-bottom:10px',

    'placeholder' => __d('admin', 'Cover')

  )

) ?>

Inside the Controller i added the follwing Code:

if ($this->request->is([‘post’, ‘put’])) {

        $page = $this->Pages->patchEntity($page,  $this->request->getData(), ['translations' => true]);

        if (!$page->errors) {

            // the process of uploading image ///

            if (isset($page->image_file) && !empty($page->image_file)) {

                $fileReserved = $this->request->getData('image_file');

                $name  = $fileReserved->getClientFilename();

                $prefixName = uniqid('image_', true);

                $changName = $prefixName . $name;

                if (!is_dir(WWW_ROOT . 'upload' . DS . 'pages')) {

                    mkdir(WWW_ROOT . 'upload' . DS . 'pages', 0775);

                }

                $targetPath = (WWW_ROOT . 'upload' . DS . 'pages' . DS . $changName);

                if ($changName) {

                    $fileReserved->moveTo($targetPath);

                }

                $page->image = 'pages/' . $changName;

            } else {

                $page->image = 'pages/default_page.jpg';

            }

        

             // the process of uploading Cover ///

             if (isset($page->cover) && !empty($page->cover)) {

                $fileReserved = $this->request->getData('cover');

                $name  = $fileReserved->getClientFilename();

                $prefixName = uniqid('image_', true);

                $changName = $prefixName . $name;

                if (!is_dir(WWW_ROOT . 'upload' . DS . 'pages')) {

                    mkdir(WWW_ROOT . 'upload' . DS . 'pages', 0775);

                }

                $targetPath = (WWW_ROOT . 'upload' . DS . 'pages' . DS . $changName);

                if ($changName) {

                    $fileReserved->moveTo($targetPath);

                }

                $page->image = 'pages/' . $changName;

            } else {

                $page->image = 'pages/default_page.jpg';

            }

        }
    }

Thanks for all

Exactly which line of code is generating that error?

Thank you sir for your reply
I see that error comes from the library inside cake PHP,
Now I am trying to remove the cover at all from all sides, it works and uploads the image but when I add the cover, it shows this error.
and if I want to edit this page and I don’t upload a picture again same error

Out of curiosity, if you remove the main image and include only the cover, does that work?

sorry, Sir for the late response
it works only in case I set the name image_file otherwise is not working .
if i keep the main image but keep it the name image_file it works and other wise when i add cover , it doesnt work

thank you sir

If it works when you have only image_file and no cover, and it fails when you have only cover and no image_file, then it’s nothing to do with having two images, and something specific to the cover field. I’d try removing all the validation for cover; if that “fixes” it, then start adding it back one bit at a time until it breaks again, then you know that’s the problem.

Thank you sir for your reply.
Now I am trying to keep both fields (image and cover ) are empty and I remove the validation, it shows me the same error.
I don’t know what to do.

Thank you

I might not have been clear. When I said to remove one or the other input, I didn’t mean to leave it empty when you submit the form, I meant to remove that input entirely from the form. This is a standard debugging technique, you remove the thing that you think is breaking it. If that eliminates the error, then add the thing back in, a bit at a time, that way you can find very specifically what piece is breaking it.

Alternately, you can debug what is happening in the marshal function. Cake’s code is just PHP code, there’s nothing magic about it, so if you’ve got an integrated debugger, you can put a breakpoint in there and look at the value. If not, then add a bit of code there to log the value. I very often find that seeing the actual data that’s present at the time of an error points me straight to the source of the problem.

1 Like

I will do that Sir.
Really Appreciate your helping
Thank you, sir