在phalcon中将多个图像插入mysql数据库

owfi6suc  于 2021-06-18  发布在  Mysql
关注(0)|答案(1)|浏览(264)

我想将多个图像插入到mysql数据库的单独列中。我可以打印所有图像名称,但无法在插入列的变量中分离图像名称。我在一次提交中插入多个图像,所以当我尝试插入图像1、2、3、4打印时,显示:“1.jpg2.jpg3.jpg4.jpg”。如何在单独的变量中分隔每个*.jpg文件?
[表格]

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

[控制器]

$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();
        }
ubby3x7f

ubby3x7f1#

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');
    }
}

相关问题