我希望在单独的列中将多个图像插入mysql数据库。我可以打印所有图像名,但无法将图像名称分隔到一个变量中,以便插入列。我在单个提交中插入多个图像,所以当我尝试插入图像1、2、3、4时,print_r显示:"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();
}发布于 2018-11-05 16:11:41
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');
}
}https://stackoverflow.com/questions/53069638
复制相似问题