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

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

plz help me

Hi Darshan,

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

Thanks,
Megan
CakePHP Community manager

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

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)

is there any example code…?
i am confused…

Maybe @dash should read this link.
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']);
}

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

OK but where should i create destination folder…?

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]: Unable to move ‘E:\xampp\tmp\php1D34.tmp’ to ‘uploads/1.jpg’ [APP/Controller\RegistrationsController.php, line 40]

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

Where Should i create folder…?

by default into your /webroot folder

I am Using Xampp Server

where should i create folder…?

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

You can use 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

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

in that what should i do…

under webroot files are not there…

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.

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.

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…