# How to upload image path will store in database and image into folder in cakephp3.6.7

**URL:** <https://discourse.cakephp.org/t/how-to-upload-image-path-will-store-in-database-and-image-into-folder-in-cakephp3-6-7/4623>\
**Category:** Need Help\
**Created:** [July 25, 2018, 10:51am UTC](https://discourse.cakephp.org/t/how-to-upload-image-path-will-store-in-database-and-image-into-folder-in-cakephp3-6-7/4623 "2018-07-25T10:51:18Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![dash](https://avatars.discourse-cdn.com/v4/letter/d/7c8e57/32.png) [@dash](https://discourse.cakephp.org/u/dash)\
**Post date:** [July 25, 2018, 10:51am UTC](https://discourse.cakephp.org/t/how-to-upload-image-path-will-store-in-database-and-image-into-folder-in-cakephp3-6-7/4623/1 "2018-07-25T10:51:18Z")

</div>

how to upload image, path will store in database and image into upload folder in cakephp3.6.7

plz help me

---

<div class="post-metadata">

**Author:** ![Megslalk](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/megslalk/32/712_2.png) [@Megslalk](https://discourse.cakephp.org/u/Megslalk)\
**Post date:** [July 25, 2018, 11:02am UTC](https://discourse.cakephp.org/t/how-to-upload-image-path-will-store-in-database-and-image-into-folder-in-cakephp3-6-7/4623/2 "2018-07-25T11:02:09Z")

</div>

Hi Darshan,

Please can you share more information with us - what you’ve tried would be a great start!

Thanks,  
Megan  
CakePHP Community manager

---

<div class="post-metadata">

**Author:** ![dash](https://avatars.discourse-cdn.com/v4/letter/d/7c8e57/32.png) [@dash](https://discourse.cakephp.org/u/dash)\
**Post date:** [July 25, 2018, 11:04am UTC](https://discourse.cakephp.org/t/how-to-upload-image-path-will-store-in-database-and-image-into-folder-in-cakephp3-6-7/4623/3 "2018-07-25T11:04:43Z")

</div>

Hi ,  
i want to upload image, in that image path is storing in database , but how to store image into seperate folder…?

---

<div class="post-metadata">

**Author:** ![Megslalk](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/megslalk/32/712_2.png) [@Megslalk](https://discourse.cakephp.org/u/Megslalk)\
**Post date:** [July 25, 2018, 11:26am UTC](https://discourse.cakephp.org/t/how-to-upload-image-path-will-store-in-database-and-image-into-folder-in-cakephp3-6-7/4623/4 "2018-07-25T11:26:01Z")

</div>

No, Cake doesn’t really support that out the box (It’s actually a standard PHP).

But there are some cool plugins to handle it ([https://github.com/friendsofcake/awesome-cakephp#files](https://github.com/friendsofcake/awesome-cakephp#files))

---

<div class="post-metadata">

**Author:** ![dash](https://avatars.discourse-cdn.com/v4/letter/d/7c8e57/32.png) [@dash](https://discourse.cakephp.org/u/dash)\
**Post date:** [July 25, 2018, 11:29am UTC](https://discourse.cakephp.org/t/how-to-upload-image-path-will-store-in-database-and-image-into-folder-in-cakephp3-6-7/4623/5 "2018-07-25T11:29:20Z")

</div>

is there any example code…?  
i am confused…

---

<div class="post-metadata">

**Author:** ![mzm](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/mzm/32/1107_2.png) [@mzm](https://discourse.cakephp.org/u/mzm)\
**Post date:** [July 25, 2018, 2:00pm UTC](https://discourse.cakephp.org/t/how-to-upload-image-path-will-store-in-database-and-image-into-folder-in-cakephp3-6-7/4623/6 "2018-07-25T14:00:32Z")

</div>

Maybe @dash should read this link.  
[http://php.net/manual/en/function.move-uploaded-file.php](http://php.net/manual/en/function.move-uploaded-file.php)

The idea, use move\_uploaded\_file() after save success.

Example

```
if ($this->Articles->save($article)) {
    //here code for process move_uploaded_file($filename, $destination)
    $this->Flash->success(__('Your article has been saved.'));
    return $this->redirect(['action' => 'index']);
}
```

---

<div class="post-metadata">

**Author:** ![rrd](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/rrd/32/113_2.png) [@rrd](https://discourse.cakephp.org/u/rrd)\
**Post date:** [July 25, 2018, 2:17pm UTC](https://discourse.cakephp.org/t/how-to-upload-image-path-will-store-in-database-and-image-into-folder-in-cakephp3-6-7/4623/7 "2018-07-25T14:17:35Z")

</div>

I use this package for uploading image: [https://github.com/Xety/Cake3-Upload](https://github.com/Xety/Cake3-Upload)

---

<div class="post-metadata">

**Author:** ![dash](https://avatars.discourse-cdn.com/v4/letter/d/7c8e57/32.png) [@dash](https://discourse.cakephp.org/u/dash)\
**Post date:** [July 26, 2018, 4:26am UTC](https://discourse.cakephp.org/t/how-to-upload-image-path-will-store-in-database-and-image-into-folder-in-cakephp3-6-7/4623/8 "2018-07-26T04:26:28Z")

</div>

OK but where should i create destination folder…?

---

<div class="post-metadata">

**Author:** ![dash](https://avatars.discourse-cdn.com/v4/letter/d/7c8e57/32.png) [@dash](https://discourse.cakephp.org/u/dash)\
**Post date:** [July 26, 2018, 5:03am UTC](https://discourse.cakephp.org/t/how-to-upload-image-path-will-store-in-database-and-image-into-folder-in-cakephp3-6-7/4623/9 "2018-07-26T05:03:21Z")

</div>

I am trying like this in cakephp 3.6.7

$profilepic=$this-\>request-\>data[‘image’][‘name’];  
move\_uploaded\_file($this-\>request-\>data[‘image’][‘tmp\_name’],“uploads/”.$this-\>request-\>data[‘image’][‘name’]);

**_I am trying to store an image in cakephp 3.6.7 I am only able to save the filename in db, however, unable to store the actual file on the older . Neeplz hrlp me…_**

\*\*

error:-  
Warning (2): move\_uploaded\_file(uploads/1.jpg): failed to open stream: No such file or directory [APP/Controller\RegistrationsController.php, line 40]

Warning (2): move\_uploaded\_file() [[function.move-uploaded-file](https://secure.php.net/function.move-uploaded-file)]: Unable to move ‘E:\xampp\tmp\php1D34.tmp’ to ‘uploads/1.jpg’ [APP/Controller\RegistrationsController.php, line 40]

---

<div class="post-metadata">

**Author:** ![rrd](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/rrd/32/113_2.png) [@rrd](https://discourse.cakephp.org/u/rrd)\
**Post date:** [July 26, 2018, 11:00am UTC](https://discourse.cakephp.org/t/how-to-upload-image-path-will-store-in-database-and-image-into-folder-in-cakephp3-6-7/4623/10 "2018-07-26T11:00:38Z")

</div>

Yes, you should create the folder and set its permissions to be writeable.

---

<div class="post-metadata">

**Author:** ![dash](https://avatars.discourse-cdn.com/v4/letter/d/7c8e57/32.png) [@dash](https://discourse.cakephp.org/u/dash)\
**Post date:** [July 26, 2018, 11:07am UTC](https://discourse.cakephp.org/t/how-to-upload-image-path-will-store-in-database-and-image-into-folder-in-cakephp3-6-7/4623/11 "2018-07-26T11:07:30Z")

</div>

> [@rrd](#):
>
> er and set its permissions

Where Should i create folder…?

---

<div class="post-metadata">

**Author:** ![rrd](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/rrd/32/113_2.png) [@rrd](https://discourse.cakephp.org/u/rrd)\
**Post date:** [July 26, 2018, 12:32pm UTC](https://discourse.cakephp.org/t/how-to-upload-image-path-will-store-in-database-and-image-into-folder-in-cakephp3-6-7/4623/12 "2018-07-26T12:32:15Z")

</div>

by default into your `/webroot` folder

---

<div class="post-metadata">

**Author:** ![dash](https://avatars.discourse-cdn.com/v4/letter/d/7c8e57/32.png) [@dash](https://discourse.cakephp.org/u/dash)\
**Post date:** [July 26, 2018, 12:36pm UTC](https://discourse.cakephp.org/t/how-to-upload-image-path-will-store-in-database-and-image-into-folder-in-cakephp3-6-7/4623/13 "2018-07-26T12:36:23Z")

</div>

I am Using Xampp Server

where should i create folder…?

---

<div class="post-metadata">

**Author:** ![rrd](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/rrd/32/113_2.png) [@rrd](https://discourse.cakephp.org/u/rrd)\
**Post date:** [July 26, 2018, 1:48pm UTC](https://discourse.cakephp.org/t/how-to-upload-image-path-will-store-in-database-and-image-into-folder-in-cakephp3-6-7/4623/14 "2018-07-26T13:48:16Z")

</div>

in your cake install thee should be a /webroot folder.

---

<div class="post-metadata">

**Author:** ![mzm](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/mzm/32/1107_2.png) [@mzm](https://discourse.cakephp.org/u/mzm)\
**Post date:** [July 26, 2018, 2:16pm UTC](https://discourse.cakephp.org/t/how-to-upload-image-path-will-store-in-database-and-image-into-folder-in-cakephp3-6-7/4623/15 "2018-07-26T14:16:49Z")

</div>

You can use [Constants](https://book.cakephp.org/3.0/en/core-libraries/global-constants-and-functions.html#core-definition-constants) Full path to the webroot. **WWW\_ROOT**

`WWW_ROOT.DS.files.DS.uploads.DS.$profilepic.”.jpg”`  
Same like …/project/webroot/files/uploads/avatar.jpg

But, folder files/ **uploads** not exist, so you must create and give permission 777

---

<div class="post-metadata">

**Author:** ![dash](https://avatars.discourse-cdn.com/v4/letter/d/7c8e57/32.png) [@dash](https://discourse.cakephp.org/u/dash)\
**Post date:** [July 27, 2018, 4:19am UTC](https://discourse.cakephp.org/t/how-to-upload-image-path-will-store-in-database-and-image-into-folder-in-cakephp3-6-7/4623/16 "2018-07-27T04:19:47Z")

</div>

> [@rrd](#):
>
> webroot

in that 4 folders are there…,  
css,js,font,img and some files…

in that what should i do…

---

<div class="post-metadata">

**Author:** ![dash](https://avatars.discourse-cdn.com/v4/letter/d/7c8e57/32.png) [@dash](https://discourse.cakephp.org/u/dash)\
**Post date:** [July 27, 2018, 4:20am UTC](https://discourse.cakephp.org/t/how-to-upload-image-path-will-store-in-database-and-image-into-folder-in-cakephp3-6-7/4623/17 "2018-07-27T04:20:51Z")

</div>

under webroot files are not there…

---

<div class="post-metadata">

**Author:** ![mzm](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/mzm/32/1107_2.png) [@mzm](https://discourse.cakephp.org/u/mzm)\
**Post date:** [July 27, 2018, 6:55am UTC](https://discourse.cakephp.org/t/how-to-upload-image-path-will-store-in-database-and-image-into-folder-in-cakephp3-6-7/4623/18 "2018-07-27T06:55:40Z")

</div>

Maybe you can ignore the files folder and just create folder uploads.

`WWW_ROOT.DS.uploads.DS.$profilepic.”.jpg”`

Depend how you manage uploaded folders and files.

---

<div class="post-metadata">

**Author:** ![devzone](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/devzone/32/1105_2.png) [@devzone](https://discourse.cakephp.org/u/devzone)\
**Post date:** [July 27, 2018, 11:04am UTC](https://discourse.cakephp.org/t/how-to-upload-image-path-will-store-in-database-and-image-into-folder-in-cakephp3-6-7/4623/19 "2018-07-27T11:04:32Z")

</div>

in your code right before move\_uploaded\_file(…

```
$myfolder= WWW_ROOT.DS.files.DS.uploads;
$dir = new Folder($myfolder, true, 0755); //creates target folder if not exists.

```

and then your code

`move_uploaded_file(($this->request->data[‘image’][‘tmp_name’], $myfolder.DS.$this->request->data[‘image’][‘name’]);`

don’t forget to write

`use Cake\Filesystem\Folder;`

on top of your controller.

---

<div class="post-metadata">

**Author:** ![dash](https://avatars.discourse-cdn.com/v4/letter/d/7c8e57/32.png) [@dash](https://discourse.cakephp.org/u/dash)\
**Post date:** [July 27, 2018, 11:12am UTC](https://discourse.cakephp.org/t/how-to-upload-image-path-will-store-in-database-and-image-into-folder-in-cakephp3-6-7/4623/20 "2018-07-27T11:12:34Z")

</div>

bro i got error

Notice (8): Use of undefined constant files - assumed ‘files’ [APP/Controller\RegistrationsController.php, line 41]  
Notice (8): Use of undefined constant uploads - assumed ‘uploads’ [APP/Controller\RegistrationsController.php, line 41]

**_but image is storing…_**

[Next page](https://discourse.cakephp.org/t/how-to-upload-image-path-will-store-in-database-and-image-into-folder-in-cakephp3-6-7/4623.md?page=2)
