php - 在phalcon中将多个图像插入mysql db
问题描述
我想在单独的列中将多个图像插入 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();
}
解决方案
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');
}
}
推荐阅读
- python - 将模型保存到数据库在 Django 中抛出 ValueError
- git - 推送源树中的其他分支
- api - 具有不同响应和请求标头的相同 CURL 帖子(本地 PC 和 Live Server)
- python - 使用 url 方案打开 python 应用程序
- ubuntu - microk8s 没有运行。使用 microk8s 检查进行更深入的检查
- postgresql - TypeORM OneToMany 与多个字段的关系
- node.js - 不允许 mw 提交登录表单的无效 csrf 令牌错误 如何清除此错误
- sql - SQL Workbench WbExport 到 Redshift 的两个位置
- angular - 当通过 Angular 从 Spring Boot 加载跨源资源时,JSESSIONID 被丢弃在浏览器中
- c# - 如何使用 INotifyDataErrorInfo 在单个方法中返回包含错误的属性?