在 API Postman 的 codeigniter 中使用 db id 验证上传多张图片
Multiple image upload with db id validation in codeigniter in API Postman
我想根据 category_id 和 vendor_id 在数据库中上传多张图片,包括使用代码进行验证-点火器
这是屏幕截图
Screenshot postman attchment
function upload_category_doc(){
$vendor_id = $this->input->post("vendor_id");
$category_id = $this->input->post("category_id");
$response = array();
if(isset($_FILES['category_doc']['name']) && $_FILES['category_doc']['name'] !=''){
$insertArr['category_doc']=$this->cat_upload('category_doc');
}
$result = $this->providerapp_model->update_data("vbs_vendor_categories",$insertArr,array('vendor_id'=>$vendor_id,'category_id'=>$category_id));
$fields = "category_id,vendor_id,category_doc";
$sql1 = "SELECT $fields FROM vbs_vendor_categories vvc WHERE vvc.vendor_id=$vendor_id AND vvc.category_id=$category_id";
$vendor_cat_data = $this->db->query($sql1)->row();
if($result){
$response['data'] = $vendor_cat_data;
$response['status'] = "success";
$response['message'] = "Category document update successfully";
}else{
$response['status'] = "fail";
$response['message'] = "Something went wrong";
}
}
和他的上传类文件的上传功能
function cat_upload($imagename){
$date = date('His');
$config['upload_path'] = 'uploads/category_doc';
$config['file_name'] = 'category_doc' . $date;
$config['allowed_types'] = 'jpg|png|bmp|pdf|doc|svg';
$this->load->library('upload',$config);
if (!$this->upload->do_upload($imagename)) {
$upload_error = array('error' => $this->upload->display_errors());
} else {
$upload_data = $this->upload->data();
$config['image_library'] = 'gd2';
$config['source_image'] = $this->upload->upload_path . $this->upload->file_name;
$config['new_image'] = $this->upload->upload_path . "/thumb/" . $this->upload->file_name;
$config['master_dim'] = 'auto';
$config['width'] = 200;
$config['height'] = 200;
$this->load->library('image_lib');
$this->image_lib->initialize($config);
$this->image_lib->resize();
return $upload_data['file_name'];
}
}
你可以在邮递员的名称字段中添加一个file_name[],你可以从那里选择多个文件
并且您必须通过 PHP 代码循环上传文件
foreach ($_FILES['fil_name'] as $key => $value) {
// your stuff
}
$setting = $this->image_settings();
$this->load->library('upload', $setting);
$total = count(@$_FILES['image']['name']);
for($i=0; $i<$total; $i++)
{
$_FILES['userfile']['name']= @$_FILES['image']['name'][$i];
$_FILES['userfile']['type']= @$_FILES['image']['type'][$i];
$_FILES['userfile']['tmp_name']= @$_FILES['image']['tmp_name'][$i];
$_FILES['userfile']['error']= @$_FILES['image']['error'][$i];
$_FILES['userfile']['size']= @$_FILES['image']['size'][$i];
$this->upload->initialize($setting);
$this->upload->do_upload();
$actual_image_data = $this->upload->data();
if(!empty($actual_image_data['is_image'])) {
@$final_files_data[] = $actual_image_data['file_name'];
$img_data['image_path'] = @$final_files_data[$i];
$this->image_model->insert_image($img_data);
} else {
$response = [
'status' => FALSE,
'message' => 'Invalid file uploaded',
];
die(json_encode($response));
}
}
我想根据 category_id 和 vendor_id 在数据库中上传多张图片,包括使用代码进行验证-点火器 这是屏幕截图
Screenshot postman attchment
function upload_category_doc(){
$vendor_id = $this->input->post("vendor_id");
$category_id = $this->input->post("category_id");
$response = array();
if(isset($_FILES['category_doc']['name']) && $_FILES['category_doc']['name'] !=''){
$insertArr['category_doc']=$this->cat_upload('category_doc');
}
$result = $this->providerapp_model->update_data("vbs_vendor_categories",$insertArr,array('vendor_id'=>$vendor_id,'category_id'=>$category_id));
$fields = "category_id,vendor_id,category_doc";
$sql1 = "SELECT $fields FROM vbs_vendor_categories vvc WHERE vvc.vendor_id=$vendor_id AND vvc.category_id=$category_id";
$vendor_cat_data = $this->db->query($sql1)->row();
if($result){
$response['data'] = $vendor_cat_data;
$response['status'] = "success";
$response['message'] = "Category document update successfully";
}else{
$response['status'] = "fail";
$response['message'] = "Something went wrong";
}
}
和他的上传类文件的上传功能
function cat_upload($imagename){
$date = date('His');
$config['upload_path'] = 'uploads/category_doc';
$config['file_name'] = 'category_doc' . $date;
$config['allowed_types'] = 'jpg|png|bmp|pdf|doc|svg';
$this->load->library('upload',$config);
if (!$this->upload->do_upload($imagename)) {
$upload_error = array('error' => $this->upload->display_errors());
} else {
$upload_data = $this->upload->data();
$config['image_library'] = 'gd2';
$config['source_image'] = $this->upload->upload_path . $this->upload->file_name;
$config['new_image'] = $this->upload->upload_path . "/thumb/" . $this->upload->file_name;
$config['master_dim'] = 'auto';
$config['width'] = 200;
$config['height'] = 200;
$this->load->library('image_lib');
$this->image_lib->initialize($config);
$this->image_lib->resize();
return $upload_data['file_name'];
}
}
你可以在邮递员的名称字段中添加一个file_name[],你可以从那里选择多个文件
并且您必须通过 PHP 代码循环上传文件
foreach ($_FILES['fil_name'] as $key => $value) {
// your stuff
}
$setting = $this->image_settings();
$this->load->library('upload', $setting);
$total = count(@$_FILES['image']['name']);
for($i=0; $i<$total; $i++)
{
$_FILES['userfile']['name']= @$_FILES['image']['name'][$i];
$_FILES['userfile']['type']= @$_FILES['image']['type'][$i];
$_FILES['userfile']['tmp_name']= @$_FILES['image']['tmp_name'][$i];
$_FILES['userfile']['error']= @$_FILES['image']['error'][$i];
$_FILES['userfile']['size']= @$_FILES['image']['size'][$i];
$this->upload->initialize($setting);
$this->upload->do_upload();
$actual_image_data = $this->upload->data();
if(!empty($actual_image_data['is_image'])) {
@$final_files_data[] = $actual_image_data['file_name'];
$img_data['image_path'] = @$final_files_data[$i];
$this->image_model->insert_image($img_data);
} else {
$response = [
'status' => FALSE,
'message' => 'Invalid file uploaded',
];
die(json_encode($response));
}
}