image - File upload in Cakephp 3.3 -
i trying store image in cakephp 3.0. able save filename in db, however, unable store actual file on server. need help
form:
echo $this->form->create('user', array('url' => array('action' => 'create'), 'enctype' => 'multipart/form-data')); echo $this->form->input('upload', array('type' => 'file'));
images controller:
*/ public function add() { $image = $this->images->newentity(); //check if image has been uploaded if(!empty($this->request->data['images']['upload']['name'])) { $file = $this->request->data['images']['upload']; //put data var easy use $ext = substr(strtolower(strrchr($file['name'], '.')), 1); //get extension $arr_ext = array('jpg', 'jpeg', 'gif'); //set allowed extensions //only process if extension valid if(in_array($ext, $arr_ext)) { //do actual uploading of file. first arg tmp name, second arg //where putting move_uploaded_file($file['tmp_name'], www_root . 'img' . $file['name']); //prepare filename database entry $this->data['images']['image'] = $file['name']; } } if ($this->request->is('post')) { $image = $this->images->patchentity($image, $this->request->data); if ($this->images->save($image)) { $this->flash->success('the image has been saved.'); return $this->redirect(['action' => 'index']); } else { $this->flash->error('the image not saved. please, try again.'); } } $this->set(compact('image')); $this->set('_serialize', ['image']); }
welcome on stackoverflow!
please check question: cakephp 3.0 uploading images
this you, it's plugin uploading images: http://cakemanager.org/docs/utils/1.0/behaviors/uploadable/
Comments
Post a Comment