Skip to content

Instantly share code, notes, and snippets.

@NitishDiwakar
Created March 29, 2018 05:41
Show Gist options
  • Save NitishDiwakar/7bbf3e6be1820d4e6f95304aef62cb14 to your computer and use it in GitHub Desktop.
Save NitishDiwakar/7bbf3e6be1820d4e6f95304aef62cb14 to your computer and use it in GitHub Desktop.
Image upload controller
if ($this->request->is('post')) {
// Start
if(!empty($this->request->data['image']['name']))
{
$fileName = $this->request->data['image']['name'];
$uploadPath = 'img/uploads/products/';
$uploadFile = $uploadPath . $fileName;
if(move_uploaded_file($this->request->data['image']['tmp_name'], $uploadFile))
{
// $this->request->data['image'] = $fileName;
$this->request->data['Product']['image'] = $fileName;
/* echo $fileName;
exit();*/
}
}
// End
$product = $this->Products->patchEntity($product, $this->request->data());
$product->image = $fileName;
// $this->request->data['Product']['image'] = $fileName;
if ($this->Products->save($product)) {
$this->Flash->success(__('The product has been saved.'));
return $this->redirect(['action' => 'index']);
}
$this->Flash->error(__('The product could not be saved. Please, try again.'));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment