gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 16:46:32 26 4
gpt4 key购买 nike

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

[表格]

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

最佳答案

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

关于php - 在 phalcon 中将多个图像插入 mysql 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53069638/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com