图像文件成功上传到文件夹,但文件名未保存在数据库中
Posted
技术标签:
【中文标题】图像文件成功上传到文件夹,但文件名未保存在数据库中【英文标题】:The image files is uploaded successfully to the folder but file name is not saved in database 【发布时间】:2021-07-15 23:49:46 【问题描述】:查看页面输入表单。我正在尝试上传带有产品图片的产品,并完成有关图片和产品详细信息的完整 crud 操作。这是产品详情和图片上传的表格。
<form action="" method="post" id="product_form">
<div class="form-group">
<label for="">Subcategory</label>
<select class="form-control input-lg" id="select_subcategory">
<option value="">Choose Subcategory</option>
<option>One</option>
<option>Two</option>
<option>Three</option>
</select>
</div>
<div class="form-group">
<label for="">Product Title</label>
<input type="text" id="product_title" class="form-control">
</div>
<div class="form-group">
<label for="">Upload Images</label>
<input class="form-control input-lg" type="file" id="prod_img" name="prod_img[]"
multiple="multiple">
</div>
<div class="form-group">
<label for="">Brand</label>
<select class="form-control input-lg" id="select_brand">
<option value="">Choose Brand</option>
<option>One</option>
<option>Two</option>
</select>
</div>
<div class="form-group">
<label for="">Color</label>
<select class="form-control input-lg" id="product_color">
<option value="">Choose Color</option>
<option>White</option>
<option>Black</option>
</select>
</div>
<div class="form-group">
<label for="">Product Size</label>
<select class="form-control input-lg" id="product_size">
<option value="">Select Size</option>
<option>Small</option>
<option>Medium</option>
</select>
</div>
<div class="form-group">
<label for="">Product Price</label>
<input type="number" id="product_price" class="form-control">
</div>
<div class="form-group">
<label for="">Discounted Price</label>
<input type="text" placeholder="INR" class="form-control" id="prod_discount_price">
</div>
<div class="form-group">
<label for="">Description</label>
<input type="text" class="form-control" id="product_description">
</div>
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary" id="add_product">Add Data</button>
</div>
查看页面上用于向服务器发送数据的 Ajax 代码。
$(document).on("click", "#add_product", function(e)
e.preventDefault();
var select_category = $("#select_category").val();
var select_subcategory = $("#select_subcategory").val();
var product_title = $("#product_title").val();
var prod_img = $("#prod_img")[0].files;
var select_brand = $("#select_brand").val();
var product_color = $("#product_color").val();
var product_size = $("#product_size").val();
var product_price = $("#product_price").val();
var prod_discount_price = $("#prod_discount_price").val();
var product_description = $("#product_description").val();
if( select_category == "" || select_subcategory == "" || product_title == "" || select_brand ==
"" || product_color == "" || product_size == "" || product_price == "" || prod_discount_price ==
"" || product_description == "" )
alert("All fields are required");
else
var fd = new FormData();
var ins = document.getElementById("prod_img").files.length;
for(var i=0; i< ins; i++)
fd.append("prod_img[]", document.getElementById("prod_img").files[i]);
fd.append("select_category", select_category);
fd.append("select_subcategory", select_subcategory);
fd.append("product_title", product_title);
fd.append("select_brand", select_brand);
fd.append("product_color", product_color);
fd.append("product_size", product_size);
fd.append("product_price", product_price);
fd.append("prod_discount_price", prod_discount_price);
fd.append("product_description", product_description);
$.ajax(
url: "<?php echo base_url(); ?>InsertProduct",
type: "post",
data: fd,
processData: false,
contentType: false,
dataType: "json",
success:function(data)
fetch();
if(data.response == "success")
$('#ProductModal').modal('hide');
$("#tbody").DataTable().destroy();
toastr["success"](data.message)
else
toastr["error"](data.message)
);
$("#product_form")[0].reset();
);
用于通过会话检查上传多个图像文件和产品详细信息的控制器代码。文件已成功上传到文件夹中。有关产品的所有详细信息已成功提交到数据库,但数据库的图像部分为空。我无法发送图像文件名。
public function InsertProduct()
if($this->session->userdata('email')=="" && $this->session->userdata('password')=="")
$this->load->view('Admin/index');
else if($this->input->is_ajax_request())
$this->form_validation->set_rules('select_category', 'Category', 'required|trim');
$this->form_validation->set_rules('select_subcategory', ' Subcategory', 'required|trim');
$this->form_validation->set_rules('product_title', 'Product Title', 'required|trim');
$this->form_validation->set_rules('select_brand', 'Product Brand', 'required|trim');
$this->form_validation->set_rules('product_color', 'Product Color', 'required|trim');
$this->form_validation->set_rules('product_size', 'Product Size', 'required|trim');
$this->form_validation->set_rules('product_price', 'Product Price', 'required|trim');
$this->form_validation->set_rules('prod_discount_price', 'Discount Price',
'required|trim');
$this->form_validation->set_rules('product_description', 'Product Description',
'required|trim');
if($this->form_validation->run() == FALSE)
$data = array('response' =>"error", 'message'=>validation_errors());
else
//$this->load->library('upload');
$img = array();
$no_of_images = count($_FILES['prod_img']['name']);
if($no_of_images > 10)
$data = array('response' =>"error", 'message'=>"10 Images allowed only");
else
for($i = 0; $i < $no_of_images; $i++)
$_FILES['img_file']['name'] = $_FILES['prod_img']['name'][$i];
$_FILES['img_file']['type'] = $_FILES['prod_img']['type'][$i];
$_FILES['img_file']['tmp_name'] = $_FILES['prod_img']['tmp_name'][$i];
$_FILES['img_file']['error'] = $_FILES['prod_img']['error'][$i];
$_FILES['img_file']['size'] = $_FILES['prod_img']['size'][$i];
$config['upload_path'] = "./ProductImages/";
$config['allowed_types'] = "jpg|jpeg|png";
$this->load->library('upload', $config);
$this->upload->initialize($config);
if($this->upload->do_upload('img_file'))
$img_data = $this->upload->data();
$data[$i]['prod_img'][] = $img_data['file_name'];
if(!empty($data))
$data = $this->input->post();
if($this->adm->InsertProduct($data))
$data = array('response'=>"success", 'message'=>"Data updated
successfully");
else
$data = array('response'=> "error", 'message'=> "failed");
echo json_encode($data);
Model Code for inserting data I have
public function InsertProduct($data)
return $this->db->insert('product',$data);
我选择了 4 个要上传的图像文件。图片已上传,我收到一条成功的消息,我有 在提到的文件夹中检查它。但图像文件名未显示在数据库中。帮助我们 谢谢
【问题讨论】:
ibb.co/JRbqbzh 数据库表图片 【参考方案1】:您需要将产品表中的图片名称保存为json_encode。
public function InsertProduct()
if($this->session->userdata('email')=="" && $this->session->userdata('password')=="")
$this->load->view('Admin/index');
else if($this->input->is_ajax_request())
$this->form_validation->set_rules('select_category', 'Category', 'required|trim');
$this->form_validation->set_rules('select_subcategory', ' Subcategory', 'required|trim');
$this->form_validation->set_rules('product_title', 'Product Title', 'required|trim');
$this->form_validation->set_rules('select_brand', 'Product Brand', 'required|trim');
$this->form_validation->set_rules('product_color', 'Product Color', 'required|trim');
$this->form_validation->set_rules('product_size', 'Product Size', 'required|trim');
$this->form_validation->set_rules('product_price', 'Product Price', 'required|trim');
$this->form_validation->set_rules('prod_discount_price', 'Discount Price',
'required|trim');
$this->form_validation->set_rules('product_description', 'Product Description',
'required|trim');
if($this->form_validation->run() == FALSE)
$data = array('response' =>"error", 'message'=>validation_errors());
else
//$this->load->library('upload');
$img = array();
$no_of_images = count($_FILES['prod_img']['name']);
if($no_of_images > 10)
$data = array('response' =>"error", 'message'=>"10 Images allowed only");
else
for($i = 0; $i < $no_of_images; $i++)
$_FILES['img_file']['name'] = $_FILES['prod_img']['name'][$i];
$_FILES['img_file']['type'] = $_FILES['prod_img']['type'][$i];
$_FILES['img_file']['tmp_name'] = $_FILES['prod_img']['tmp_name'][$i];
$_FILES['img_file']['error'] = $_FILES['prod_img']['error'][$i];
$_FILES['img_file']['size'] = $_FILES['prod_img']['size'][$i];
$config['upload_path'] = "./ProductImages/";
$config['allowed_types'] = "jpg|jpeg|png";
$this->load->library('upload', $config);
$this->upload->initialize($config);
if($this->upload->do_upload('img_file'))
$img_data = $this->upload->data();
$img[] = $img_data['file_name'];
if(count($img) > 0)
$data['prod_img'] = json_encode($img);
if(!empty($data))
$data = $this->input->post();
if($this->adm->InsertProduct($data))
$data = array('response'=>"success", 'message'=>"Data updated
successfully");
else
$data = array('response'=> "error", 'message'=> "failed");
echo json_encode($data);
public function InsertProduct($data)
return $this->db->insert('product',$data);
【讨论】:
我已按照您的建议进行了尝试,但图像再次上传到文件夹但不在数据库中。我有错误。 ibb.co/vhr344R 你能打印 $data 并查看输出吗? ibb.co/wWhzSkX 是的,我得到了输出,你可以检查我已经打印了输出 共享产品表和图片表的数据库架构。 如果您想在产品表中保存图片名称,那么您必须将它们保存为 json 编码。以上是关于图像文件成功上传到文件夹,但文件名未保存在数据库中的主要内容,如果未能解决你的问题,请参考以下文章
railscarrierwave - 图像网址保存在数据库中但文件未保存
通过 wp_handle_upload 删除未放入上传文件夹的图像