Search code examples
phpmysqlphalcon

Insert Multiple Image into mysql db in phalcon


I'm want to insert multiple image into mysql database in separate column. i can print all image name but unable to separate the images name in a variable for insert into column. i'm inserting multiple image in single submit so when i try to insert image 1,2,3,4 print_r shows: "1.jpg2.jpg3.jpg4.jpg". How could i separate each *.jpg file in separate variable?

[FORM]

<input type="file" name="product_image[]" value="" multiple="multiple" />

[CONTROLLER]

        $insert = new Products();

        if(is_array($this->request->getUploadedFiles())){
            $image = $this->request->getUploadedFiles();
            foreach($image as $file)
            {
                $file->moveTo('uploads/shop/' . $file->getName());
                $myvars[] = $file->getName();
            }

            $insert->product_image1 = $myvars[0]);
            $insert->product_image2 = $myvars[1]);
            $insert->product_image3 = $myvars[2]);
            $insert->product_image4 = $myvars[3]);
            $insert->product_image5 = $myvars[4]);
            $insert->save();
        }

Solution

  • public function insertAction()
    {
        $insert = new Products();
    
        if($this->request->hasFiles(true) == true)
        {
            $image = $this->request->getUploadedFiles();
            foreach($image as $file)
            {
                if($this->imageCheck($file->getRealType()))
                {
                    $file->moveTo('uploads/shop/' . $file->getName());
    
                    $image = new Imagick('uploads/shop/' . $file->getName());   
                    #Resize & Crop Image
                    if($image->getWidth() >= 501 || $image->getHeight() >= 501)
                    {                        
                        $image->resize(500,500);
                        $image->crop(500,500,0,0);
                    }   
                    $image->text('FireFly', TRUE, TRUE, 100, '#ddd', 30, ''); 
                    $image->render(NULL, 60); 
                    $image->sharpen(10);
                    $imgName = md5(uniqid(rand(), true)).strtolower(date('-dmy-').$file->getName());
                    $image->save($_SERVER['DOCUMENT_ROOT'] . '/shopping/public/uploads/shop/'.$imgName);
                    unlink('uploads/shop/' . $file->getName());                        
                }
                else
                {
                    $this->flashSession->error("ERROR:: File extension not allowed");
                    return $this->response->redirect($this->router->getControllerName());                    
                }   
                #Get Image Array() into a variable                 
                $myvars[] = $imgName;
            }
                $insert->pimg_front = empty($myvars[0]) ? "empty.png" : $myvars[0];
                $insert->pimg_back = empty($myvars[1]) ? "empty.png" : $myvars[1];
                $insert->pimg_top = empty($myvars[2]) ? "empty.png" : $myvars[2];
                $insert->pimg_left = empty($myvars[3]) ? "empty.png" : $myvars[3];
                $insert->pimg_right = empty($myvars[4]) ? "empty.png" : $myvars[4];
                $insert->pimg_bottom = empty($myvars[5]) ? "empty.png" : $myvars[5];
        }
        else{
                $insert->pimg_front = 'empty.png';
                $insert->pimg_back = 'empty.png';
                $insert->pimg_top = 'empty.png';
                $insert->pimg_left = 'empty.png';
                $insert->pimg_right = 'empty.png';
                $insert->pimg_bottom = 'empty.png';            
        }
    
    
        if($insert->save() == true){
            print_r('OK');
        }else{
           print_r('NO');
        }
    }