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

**URL:** <https://discourse.cakephp.org/t/uploading-two-pictures-in-one-form-cake-php-version-4-x/8547>\
**Category:** Need Help\
**Created:** [October 26, 2020, 5:29pm UTC](https://discourse.cakephp.org/t/uploading-two-pictures-in-one-form-cake-php-version-4-x/8547 "2020-10-26T17:29:45Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![jamal-alnaeb](https://avatars.discourse-cdn.com/v4/letter/j/ed655f/32.png) [@jamal-alnaeb](https://discourse.cakephp.org/u/jamal-alnaeb)\
**Post date:** [October 26, 2020, 5:29pm UTC](https://discourse.cakephp.org/t/uploading-two-pictures-in-one-form-cake-php-version-4-x/8547/1 "2020-10-26T17:29:45Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Zuluru](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/zuluru/32/1230_2.png) [@Zuluru](https://discourse.cakephp.org/u/Zuluru)\
**Post date:** [October 26, 2020, 5:52pm UTC](https://discourse.cakephp.org/t/uploading-two-pictures-in-one-form-cake-php-version-4-x/8547/2 "2020-10-26T17:52:18Z")

</div>

Exactly which line of code is generating that error?

---

<div class="post-metadata">

**Author:** ![jamal-alnaeb](https://avatars.discourse-cdn.com/v4/letter/j/ed655f/32.png) [@jamal-alnaeb](https://discourse.cakephp.org/u/jamal-alnaeb)\
**Post date:** [October 26, 2020, 5:58pm UTC](https://discourse.cakephp.org/t/uploading-two-pictures-in-one-form-cake-php-version-4-x/8547/3 "2020-10-26T17:58:28Z")

</div>

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

 ![error](https://canada1.discourse-cdn.com/flex029/uploads/cakephp/original/2X/5/5334cc5bfa35f49c4500ee8e5521a729b2173461.png)

---

<div class="post-metadata">

**Author:** ![Zuluru](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/zuluru/32/1230_2.png) [@Zuluru](https://discourse.cakephp.org/u/Zuluru)\
**Post date:** [October 26, 2020, 6:33pm UTC](https://discourse.cakephp.org/t/uploading-two-pictures-in-one-form-cake-php-version-4-x/8547/5 "2020-10-26T18:33:01Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jamal-alnaeb](https://avatars.discourse-cdn.com/v4/letter/j/ed655f/32.png) [@jamal-alnaeb](https://discourse.cakephp.org/u/jamal-alnaeb)\
**Post date:** [October 27, 2020, 12:33pm UTC](https://discourse.cakephp.org/t/uploading-two-pictures-in-one-form-cake-php-version-4-x/8547/6 "2020-10-27T12:33:10Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Zuluru](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/zuluru/32/1230_2.png) [@Zuluru](https://discourse.cakephp.org/u/Zuluru)\
**Post date:** [October 27, 2020, 2:31pm UTC](https://discourse.cakephp.org/t/uploading-two-pictures-in-one-form-cake-php-version-4-x/8547/7 "2020-10-27T14:31:33Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![jamal-alnaeb](https://avatars.discourse-cdn.com/v4/letter/j/ed655f/32.png) [@jamal-alnaeb](https://discourse.cakephp.org/u/jamal-alnaeb)\
**Post date:** [October 27, 2020, 2:48pm UTC](https://discourse.cakephp.org/t/uploading-two-pictures-in-one-form-cake-php-version-4-x/8547/8 "2020-10-27T14:48:43Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Zuluru](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/zuluru/32/1230_2.png) [@Zuluru](https://discourse.cakephp.org/u/Zuluru)\
**Post date:** [October 27, 2020, 3:34pm UTC](https://discourse.cakephp.org/t/uploading-two-pictures-in-one-form-cake-php-version-4-x/8547/9 "2020-10-27T15:34:58Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![jamal-alnaeb](https://avatars.discourse-cdn.com/v4/letter/j/ed655f/32.png) [@jamal-alnaeb](https://discourse.cakephp.org/u/jamal-alnaeb)\
**Post date:** [October 27, 2020, 3:56pm UTC](https://discourse.cakephp.org/t/uploading-two-pictures-in-one-form-cake-php-version-4-x/8547/10 "2020-10-27T15:56:46Z")

</div>

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