我在这里有一个问题,我在opencart上建立一个简单的多重上传功能,文件保存在文件夹但不保存路径n mysql,任何人都可以帮助我,
查看:
<input type="file" name="picture[]" accept="img/*" />
<input type="file" name="picture[]" accept="img/*" />
控制器:
foreach ($this->request->files['picture']['error'] as $key => $error) {
$tmp_name = $this->request->files['picture']['tmp_name'][$key];
$this->request->post['img'] = "test/".$this->request->files['picture']['name'][$key];
move_uploaded_file($tmp_name,DIR_IMAGE .$this->request->post['img']);
}
型号:
$this->db->query("INSERT INTO " . DB_PREFIX . "product_image SET image = '" . $this->db->escape($data['img']) . "',product_id = '" . (int)$product_id . "'");
答案 0 :(得分:0)
public function create(){
global $database;
if(isset($_FILES['fupload'])){
$data = $_FILES["fupload"]["error"];
// loops through shops that has been selected and store category associated with store
foreach ($_FILES["fupload"]["error"] as $key => $error) {
//checks if there is no error
if ($error == UPLOAD_ERR_OK) {
move_uploaded_file($_FILES['fupload']['tmp_name'][$key],"../public/gallery/".basename($_FILES['fupload']['name'][$key]));
$image = new Imageresize(); // an instance of image resize object
$image->load("../public/gallery/".basename($_FILES['fupload']['name'][$key]));
//$image->image =;
$image->resize(400,400);
$image->save("../public/gallery/".basename($_FILES['fupload']['name'][$key]));
//this section is needed to get the extension for image type in renaming the image
if ($_FILES['fupload']['type'][$key]=="image/gif"){
$ext = ".gif";
}
if ($_FILES['fupload']['type'][$key]=="image/png"){
$ext = ".png";
}
if ($_FILES['fupload']['type'][$key]=="image/jpeg"){
$ext = ".jpeg";
}
if ($_FILES['fupload']['type'][$key]=="image/pjpeg"){
$ext = ".jpeg";
}
if ($_FILES['fupload']['type'][$key]=="image/gif"){
$ext = ".gif";
}
if ($_FILES['fupload']['type'][$key]=="image/jpg"){
$ext = ".jpg";
}
$new_name = uniqid()."_".time().$ext; //new name for the image
rename("../public/gallery/".basename($_FILES['fupload']['name'][$key]),"../public/gallery/".$new_name);
$photo = $new_name;
$sqlImg = "INSERT INTO gallery (img_url) VALUES ('$photo') ";
$Result = $database->db_query($sqlImg);
}
}
}
}
您可以查看此代码我使用imageresize来调整上传图像的大小并覆盖您可以跳过该部分并按照循环上传图像
答案 1 :(得分:0)
几周前我修改了当前的OpenCart上一版本(2.0.1.1)以支持使用DropZone.js上传多个图像。我正在公开这个项目,所以你可以从这里下载文件: https://github.com/oscaralderete/opencart-multiple-image-uploader