image upload in cake 2

Solutions on MaxInterview for image upload in cake 2 by the best coders in the world

showing results for - "image upload in cake 2"
Marta
03 May 2016
1public function add() {
2    if ($this->request->is('post')) {
3        $this->Category->create();
4        //Check if image has been uploaded
5        if (!empty($this->request->data['Category']['upload']['name'])) {
6            $file = $this->request->data['Category']['upload'];
7
8            $ext = substr(strtolower(strrchr($file['name'], '.')), 1);
9            $arr_ext = array('jpg', 'jpeg', 'gif','png');
10
11            if (in_array($ext, $arr_ext)) {
12                move_uploaded_file($file['tmp_name'], WWW_ROOT . 'img/webimages/categories/' . $file['name']);
13                //prepare the filename for database entry
14                $this->request->data['Category']['image'] = $file['name'];
15            }
16        }
17
18        if ($this->Category->save($this->request->data)) {
19            $this->Session->setFlash(__('The category has been saved.'));
20            return $this->redirect(array('action' => 'index'));
21        } else {
22            $this->Session->setFlash(__('The category could not be saved. Please, try again.'));
23        }
24    }
25    $parentCategories = $this->Category->ParentCategory->find('list');
26    $categoriesStatus = $this->Category->getCategoriesStatus();//model's method to get list of status
27    $this->set(compact('parentCategories', 'categoriesStatus'));
28}
Valerio
13 Mar 2017
1   public function add() {
2            if ($this->request->is('post')) {
3                $this->Product->create();
4                if ($this->Product->save($this->request->data)) {
5                    $this->Session->setFlash(__('The product has been saved.'));
6                    return $this->redirect(array('action' => 'index'));
7                } else {
8                    $this->Session->setFlash(__('The product could not be saved. Please, try again.'));
9                }
10                if(!empty($this->data))
11                {
12                    //Check if image has been uploaded
13                    if(!empty($this->data['products']['upload']['name']))
14                    {
15                        $file = $this->data['products']['upload']; //put the data into a var for easy use
16
17                        $ext = substr(strtolower(strrchr($file['name'], '.')), 1); //get the extension
18                        $arr_ext = array('jpg', 'jpeg', 'gif'); //set allowed extensions
19
20                        //only process if the extension is valid
21                        if(in_array($ext, $arr_ext))
22                        {
23                            //do the actual uploading of the file. First arg is the tmp name, second arg is
24                            //where we are putting it
25                            move_uploaded_file($file['tmp_name'], WWW_ROOT . 'CakePHP/app/webroot/img/' . $file['name']);
26
27                            //prepare the filename for database entry
28                            $this->data['products']['product_image'] = $file['name'];
29                        }
30                    }
31
32                    //now do the save
33                    $this->products->save($this->data) ;
34                }
35            }
36
37        }