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

**URL:** <https://discourse.cakephp.org/t/object-of-class-laminas-diactoros-uploadedfile-could-not-be-converted-to-string/9312>\
**Category:** Need Help\
**Created:** [May 2, 2021, 1:25am UTC](https://discourse.cakephp.org/t/object-of-class-laminas-diactoros-uploadedfile-could-not-be-converted-to-string/9312 "2021-05-02T01:25:51Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![makamo66](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/makamo66/32/2156_2.png) [@makamo66](https://discourse.cakephp.org/u/makamo66)\
**Post date:** [May 2, 2021, 1:25am UTC](https://discourse.cakephp.org/t/object-of-class-laminas-diactoros-uploadedfile-could-not-be-converted-to-string/9312/1 "2021-05-02T01:25:51Z")

</div>

I have a form that allows an image file to be uploaded. When I upload an image, it successfully submits it to the database but when no image is uploaded, I get the error “Object of class Laminas\Diactoros\UploadedFile could not be converted to string”

```auto
public function add($user_id = null)
	{
$recipe = $this->Recipes->newEmptyEntity();
$this->set(compact('recipe'));
$image1 = $this->request->getUploadedFile('image1'); 
if ($image1 !== null ){
$destination2 = "img/recipe_images/" . $image1->getClientFilename();
$image1->moveTo($destination2);
$recipe -> image1 = $destination2;
} 
if ($this->Recipes->save($recipe)) {
$this->Flash->success(__('The recipe has been saved.'));
return $this->redirect(['action' => 'view', $recipe->id]);
}
$this->Flash->error(__('The recipe could not be saved. Please, try again.'));
}
}

```

---

<div class="post-metadata">

**Author:** ![dirk](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/dirk/32/2174_2.png) [@dirk](https://discourse.cakephp.org/u/dirk)\
**Post date:** [May 2, 2021, 2:00pm UTC](https://discourse.cakephp.org/t/object-of-class-laminas-diactoros-uploadedfile-could-not-be-converted-to-string/9312/2 "2021-05-02T14:00:50Z")

</div>

> [@makamo66](#):
>
> `if ($image1 !== null ){`

You better check if $image1 got errors. As far as i remember.it will never be NULL.  
Tomorrow i can give you more details, when im home at my computer

---

<div class="post-metadata">

**Author:** ![makamo66](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/makamo66/32/2156_2.png) [@makamo66](https://discourse.cakephp.org/u/makamo66)\
**Post date:** [May 2, 2021, 2:29pm UTC](https://discourse.cakephp.org/t/object-of-class-laminas-diactoros-uploadedfile-could-not-be-converted-to-string/9312/3 "2021-05-02T14:29:25Z")

</div>

I should have included my entire code. This is what it looks like now:

```auto
public function add($user_id = null)
    {
$recipe = $this->Recipes->newEmptyEntity();
$this->set(compact('recipe'));
if ($this->request->is('post')) {
$recipe = $this->Recipes->patchEntity($recipe, $this->request->getData());
if ($this->Recipes->save($recipe)) {
$image1 = $this->request->getUploadedFile('image1'); 
if ($image1 !== 0){ $destination2 = "img/recipe_images/" . $image1->getClientFilename();
if (file_exists($destination2)){
$image1->moveTo($destination2);
$recipe -> image1 = $destination2;
}}

```

If I comment out

```auto
$recipe = $this->Recipes->patchEntity($recipe, $this->request->getData());

```

it doesn’t throw an error but no data is saved to the database.

---

<div class="post-metadata">

**Author:** ![dirk](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/dirk/32/2174_2.png) [@dirk](https://discourse.cakephp.org/u/dirk)\
**Post date:** [May 2, 2021, 2:54pm UTC](https://discourse.cakephp.org/t/object-of-class-laminas-diactoros-uploadedfile-could-not-be-converted-to-string/9312/4 "2021-05-02T14:54:00Z")

</div>

> [@makamo66](#):
>
> `$recipe -> image1 = $destination2;`

when you assign $destination2 to $recipe is to late. at this moment $recipe is already saved.  
tomorrow i will send you more details

---

<div class="post-metadata">

**Author:** ![makamo66](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/makamo66/32/2156_2.png) [@makamo66](https://discourse.cakephp.org/u/makamo66)\
**Post date:** [May 2, 2021, 3:59pm UTC](https://discourse.cakephp.org/t/object-of-class-laminas-diactoros-uploadedfile-could-not-be-converted-to-string/9312/5 "2021-05-02T15:59:20Z")

</div>

I changed the order as you pointed out so it saves at the right time. I added the following and now it works:

`if ($image1 !== null && $image1->getError() !== UPLOAD_ERR_NO_FILE)`

---

<div class="post-metadata">

**Author:** ![makamo66](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/makamo66/32/2156_2.png) [@makamo66](https://discourse.cakephp.org/u/makamo66)\
**Post date:** [May 2, 2021, 4:21pm UTC](https://discourse.cakephp.org/t/object-of-class-laminas-diactoros-uploadedfile-could-not-be-converted-to-string/9312/6 "2021-05-02T16:21:07Z")

</div>

It was ‘uploadedFilesAsObjects’ =\> false, saved to config/app.php that solved the problem but now I have to click on the submit button twice before it saves to the database.

---

<div class="post-metadata">

**Author:** ![makamo66](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/makamo66/32/2156_2.png) [@makamo66](https://discourse.cakephp.org/u/makamo66)\
**Post date:** [May 2, 2021, 4:23pm UTC](https://discourse.cakephp.org/t/object-of-class-laminas-diactoros-uploadedfile-could-not-be-converted-to-string/9312/7 "2021-05-02T16:23:56Z")

</div>

Now if the image file isn’t empty, I have to click the submit button twice. I think I am closing the curly bracket in the wrong place.

---

<div class="post-metadata">

**Author:** ![makamo66](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/makamo66/32/2156_2.png) [@makamo66](https://discourse.cakephp.org/u/makamo66)\
**Post date:** [May 2, 2021, 4:41pm UTC](https://discourse.cakephp.org/t/object-of-class-laminas-diactoros-uploadedfile-could-not-be-converted-to-string/9312/8 "2021-05-02T16:41:07Z")

</div>

Where should my curly brackets be? I have to submit twice for the following code:

```auto
public function add($user_id = null)
    {
$recipe = $this->Recipes->newEmptyEntity();
$this->set(compact('recipe'));
if ($this->request->is('post')) {
$recipe = $this->Recipes->patchEntity($recipe, $this->request->getData());
$image1=$this->request->getUploadedFile('image1'); 
if
($image1 !== null && $image1->getError() !== UPLOAD_ERR_NO_FILE){ 
$destination2 = "img/recipe_images/" . $image1->getClientFilename();
$image1->moveTo($destination2);
$recipe -> image1 = $destination2;
}
if ($this->Recipes->save($recipe)) {
$this->Flash->success(__('The recipe has been saved.'));
return $this->redirect(['action' => 'view', $recipe->id]);
}
$this->Flash->error(__('The recipe could not be saved. Please, try again.'));
        }

    }

```

---

<div class="post-metadata">

**Author:** ![makamo66](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/makamo66/32/2156_2.png) [@makamo66](https://discourse.cakephp.org/u/makamo66)\
**Post date:** [May 2, 2021, 4:42pm UTC](https://discourse.cakephp.org/t/object-of-class-laminas-diactoros-uploadedfile-could-not-be-converted-to-string/9312/9 "2021-05-02T16:42:54Z")

</div>

It’s not saving the image to the database any more. I thought it was submitting it at one time but maybe I’m wrong.

---

<div class="post-metadata">

**Author:** ![makamo66](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/makamo66/32/2156_2.png) [@makamo66](https://discourse.cakephp.org/u/makamo66)\
**Post date:** [May 2, 2021, 5:23pm UTC](https://discourse.cakephp.org/t/object-of-class-laminas-diactoros-uploadedfile-could-not-be-converted-to-string/9312/10 "2021-05-02T17:23:47Z")

</div>

I need both `($image1 !== null && $image1->getError() !== UPLOAD_ERR_NO_FILE)` in the function and `‘uploadedFilesAsObjects’ => false`, saved to config/app.php in order for it to not submit an error but it doesn’t save the image to the database.

---

<div class="post-metadata">

**Author:** ![makamo66](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/makamo66/32/2156_2.png) [@makamo66](https://discourse.cakephp.org/u/makamo66)\
**Post date:** [May 2, 2021, 8:20pm UTC](https://discourse.cakephp.org/t/object-of-class-laminas-diactoros-uploadedfile-could-not-be-converted-to-string/9312/11 "2021-05-02T20:20:06Z")

</div>

The following saves the image but it doesn’t save it to the target path. I’ve tried two separate ways to configure the target path and neither one works.

```auto
$image1 = $this->request->getUploadedFile('image1');
$fileName = $image1->getClientFilename();
//$targetPath = WWW_ROOT . 'img'. DS . 'recipe_images' .DS. $fileName;
$targetPath = 'img/recipe_images/' . $fileName;
$image1->moveTo($targetPath);
$recipe = $this->Recipes->patchEntity($recipe, $this->request->getData());  
$recipe->image1 = $fileName;

```

---

<div class="post-metadata">

**Author:** ![makamo66](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/makamo66/32/2156_2.png) [@makamo66](https://discourse.cakephp.org/u/makamo66)\
**Post date:** [May 2, 2021, 8:40pm UTC](https://discourse.cakephp.org/t/object-of-class-laminas-diactoros-uploadedfile-could-not-be-converted-to-string/9312/12 "2021-05-02T20:40:36Z")

</div>

I tried the following as well but it still didn’t save my image to the target path: `move_uploaded_file($fileName , $targetPath);`

---

<div class="post-metadata">

**Author:** ![makamo66](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/makamo66/32/2156_2.png) [@makamo66](https://discourse.cakephp.org/u/makamo66)\
**Post date:** [May 2, 2021, 10:36pm UTC](https://discourse.cakephp.org/t/object-of-class-laminas-diactoros-uploadedfile-could-not-be-converted-to-string/9312/13 "2021-05-02T22:36:27Z")

</div>

It turns out that the image is actually being saved to the target path directory and it just doesn’t show the target path directory in the database so this is working now.

---

<div class="post-metadata">

**Author:** ![makamo66](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/makamo66/32/2156_2.png) [@makamo66](https://discourse.cakephp.org/u/makamo66)\
**Post date:** [May 2, 2021, 11:04pm UTC](https://discourse.cakephp.org/t/object-of-class-laminas-diactoros-uploadedfile-could-not-be-converted-to-string/9312/14 "2021-05-02T23:04:40Z")

</div>

It is saving the image but now the problem is that I can’t display the image on the template page because my earlier saves included the directory. I can do a str\_replace() to filter out the directory saved and then concatenate the directory name to the image but this isn’t working for me for some reason.

---

<div class="post-metadata">

**Author:** ![makamo66](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/makamo66/32/2156_2.png) [@makamo66](https://discourse.cakephp.org/u/makamo66)\
**Post date:** [May 2, 2021, 11:24pm UTC](https://discourse.cakephp.org/t/object-of-class-laminas-diactoros-uploadedfile-could-not-be-converted-to-string/9312/15 "2021-05-02T23:24:11Z")

</div>

I just added `$fileName = 'img/recipe_images/' . $fileName;` and now the directory name is in the database.

---

<div class="post-metadata">

**Author:** ![makamo66](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/makamo66/32/2156_2.png) [@makamo66](https://discourse.cakephp.org/u/makamo66)\
**Post date:** [May 2, 2021, 11:44pm UTC](https://discourse.cakephp.org/t/object-of-class-laminas-diactoros-uploadedfile-could-not-be-converted-to-string/9312/16 "2021-05-02T23:44:41Z")

</div>

I’m back to square one. It only works when a file is uploaded. If no file is uploaded, I get the error: Cannot retrieve stream due to upload error: No file was uploaded

---

<div class="post-metadata">

**Author:** ![makamo66](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/makamo66/32/2156_2.png) [@makamo66](https://discourse.cakephp.org/u/makamo66)\
**Post date:** [May 3, 2021, 12:01am UTC](https://discourse.cakephp.org/t/object-of-class-laminas-diactoros-uploadedfile-could-not-be-converted-to-string/9312/17 "2021-05-03T00:01:30Z")

</div>

I’m sorry if I’m not clearer about what is happening. I keep changing the code and then I don’t add it to the replies that I make.

---

<div class="post-metadata">

**Author:** ![dirk](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/dirk/32/2174_2.png) [@dirk](https://discourse.cakephp.org/u/dirk)\
**Post date:** [May 3, 2021, 5:38am UTC](https://discourse.cakephp.org/t/object-of-class-laminas-diactoros-uploadedfile-could-not-be-converted-to-string/9312/18 "2021-05-03T05:38:27Z")

</div>

```
public function add($user_id = null) {
  $recipe = $this->Recipes->newEmptyEntity();
  $this->set(compact('recipe'));
  if ($this->request->is('post')) 
  {
      $recipe = $this->Recipes->patchEntity($recipe, $this->request->getData());
      if(!$recipe->getErrors()) {

          if (!empty($this->request->getData('image1'))) {
          $image1 = $this->request->getData('image_file');
              if ($image1->getError() == 0) {
                $destination2 = WWW_ROOT.'img'.DS.'recipe_images'.DS.$image1->getClientFilename();
                $recipe->image1=$image1->getClientFilename(); //here i save only the filename, not the whole path
                $image->moveTo($destination);
              }
              else {
                  $recipe->image1=NULL; //remove this for the edit method, otherwise, it could happen that the filename will be overwritten with NULL
              }
          }
        }
      if ($this->Recipes->save($recipe)) 
      {
        $this->Flash->success(__('The recipe has been saved.'));
        return $this->redirect(['action' => 'view', $recipe->id]);
      }
      $this->Flash->error(__('The recipe could not be saved. Please, try again.'));
  }
}
```

---

<div class="post-metadata">

**Author:** ![Jawfin](https://avatars.discourse-cdn.com/v4/letter/j/dbc845/32.png) [@Jawfin](https://discourse.cakephp.org/u/Jawfin)\
**Post date:** [May 3, 2021, 5:44am UTC](https://discourse.cakephp.org/t/object-of-class-laminas-diactoros-uploadedfile-could-not-be-converted-to-string/9312/19 "2021-05-03T05:44:49Z")

</div>

Will there be a problem with that code when set’ing it before changes are made to it?  
Admittedly the changes are applied from a post call, therefore the var won’t be going back to the client, it just may not be future-proof in case that is changed (or copy/pasted for a template elsewhere).

---

<div class="post-metadata">

**Author:** ![makamo66](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/makamo66/32/2156_2.png) [@makamo66](https://discourse.cakephp.org/u/makamo66)\
**Post date:** [May 3, 2021, 1:12pm UTC](https://discourse.cakephp.org/t/object-of-class-laminas-diactoros-uploadedfile-could-not-be-converted-to-string/9312/20 "2021-05-03T13:12:44Z")

</div>

Thanks for the code, dirk.  
I corrected the typos so that the code includes the line  
`$image1 = $this->request->getData('image1');`  
instead of  
`$image1 = $this->request->getData('image_file');`  
and  
`$image1->moveTo($destination2);`  
instead of  
`$image->moveTo($destination);`

When I didn’t upload an image, I got the error: Call to a member function getError() on array

and when I did upload a file it stayed on the same page after clicking the submit button.  
So I changed  
`$image1 = $this->request->getData('image1');`  
to  
`$image1 = $this->request->getUploadedFile('image1');`  
but it still stayed on the same page after I clicked on the submit button and nothing was saved to the database.

[Next page](https://discourse.cakephp.org/t/object-of-class-laminas-diactoros-uploadedfile-could-not-be-converted-to-string/9312.md?page=2)
