我想停止选择多个图像,而只选择单个图像。在此示例中,我在codeigniter中处理图像上载,我可以选择多个图像,但是我只需要选择扔掉的单个图像即可。所以我该如何更改选择以选择单个图像。同样在这个例子中,当我们编辑上传的图像时,旧的图像将被从数据库中删除,我想改变它,但是我不知道在哪里以及如何改变它的代码。
这是我的控制器。
public function index()
{
$getdata = $this->db->query("SELECT p.id,p.product_description,pd.product_name FROM g_product AS p INNER JOIN g_product_detail AS pd ON p.id = pd.product_id GROUP BY p.id ORDER BY p.id ASC");
$this->load->view('header');
$this->load->view('gallery2/index',array('data' => $getdata));
$this->load->view('footer');
}
public function form()
{
$this->load->view('gallery2/form', array('error' => ''));
}
public function delete($id)
{
$getpic = $this->db->query("SELECT full_path FROM g_product_detail WHERE product_id = '{$id}'");
foreach($getpic->result() as $row)
{
unlink($row->full_path);
}
$this->db->where('product_id',$id);
$this->db->delete('g_product_detail');
$this->db->where('id',$id);
$this->db->delete('g_product');
redirect('gallery2/index');
}
private function setup_upload_option()
{
$config = array();
$config['upload_path'] = './uploadmulti';
$config['allowed_types'] = 'jpg|png|gif';
$config['encrypt_name'] = TRUE;
$config['overwrite'] = False;
return $config;
}
public function edit($id)
{
$getrow = $this->db->query("SELECT * FROM g_product WHERE id= '{$id}' ");
$row = $getrow->row();
$getdetail = $this->db->query("SELECT * FROM g_product_detail WHERE product_id = '{$id}'");
$this->load->view('header');
$this->load->view('gallery2/edit',array('row' => $row,'result' => $getdetail));
$this->load->view('footer');
}
public function do_update()
{
$id = $this->input->post('id');
$data = array(
'product_description' => $this->input->post('product_description')
);
$this->db->where('id',$id);
$te = $this->db->update('g_product',$data);
if(!empty($_FILES['userfile']['name'][0]))
{
$getpic = $this->db->query("SELECT full_path FROM g_product_detail WHERE product_id = '{$id}'");
foreach($getpic->result() as $row)
{
unlink($row->full_path);
}
$this->db->where('product_id',$id);
$this->db->delete('g_product_detail');
$this->load->library('upload');
$files = $_FILES;
$count = count($_FILES['userfile']['name']);
for($i=0;$i<$count;$i++)
{
$_FILES['userfile']['name'] = $files['userfile']['name'][$i];
$_FILES['userfile']['type'] = $files['userfile']['type'][$i];
$_FILES['userfile']['tmp_name'] = $files['userfile']['tmp_name'][$i];
$_FILES['userfile']['size'] = $files['userfile']['size'][$i];
$_FILES['userfile']['error'] = $files['userfile']['error'][$i];
$this->upload->initialize($this->setup_upload_option());
if($this->upload->do_upload() == False)
{
$error = array('error' => $this->upload->display_errors());
$this->load->view('gallery2/form', $error);
}
else
{
$data = $this->upload->data();
$dataarray = array(
'product_id' => $id,
'product_name' =>$data['file_name'],
'product_size' =>$data['file_size'],
'product_ext' =>$data['file_ext'],
'full_path' =>$data['full_path']
);
$this->db->insert('g_product_detail',$dataarray);
}
}
redirect("gallery2/index");
}
else
{
redirect("gallery2/index");
}
}
public function do_upload()
{
$this->load->library('upload');
$files = $_FILES;
$data = array(
'product_description' => $this->input->post('product_description')
);
$ch = $this->db->insert('g_product',$data);
$id = $this->db->insert_id();
if($ch > 0)
{
$count = count($_FILES['userfile']['name']);
for($i=0;$i<$count;$i++)
{
$_FILES['userfile']['name'] = $files['userfile']['name'][$i];
$_FILES['userfile']['type'] = $files['userfile']['type'][$i];
$_FILES['userfile']['tmp_name'] = $files['userfile']['tmp_name'][$i];
$_FILES['userfile']['size'] = $files['userfile']['size'][$i];
$_FILES['userfile']['error'] = $files['userfile']['error'][$i];
$this->upload->initialize($this->setup_upload_option());
if($this->upload->do_upload() == False)
{
$error = array('error' => $this->upload->display_errors());
$this->load->view('gallery2/form', $error);
}
else
{
$data = $this->upload->data();
$dataarray = array(
'product_id' => $id,
'product_name' =>$data['file_name'],
'product_size' =>$data['file_size'],
'product_ext' =>$data['file_ext'],
'full_path' =>$data['full_path']
);
$this->db->insert('g_product_detail',$dataarray);
}
}
redirect('gallery2/index');
}
}
我想停止选择多个图像,而只选择单个图像。
这似乎在文件的视图部分中。如果我是正确的,则解决方案将更<input type="file" name="userfile" multiple>
改为<input type="file" name="userfile">
。这应该可以解决您的多张图片选择问题。
简而言之,multiple
从文件输入中删除属性(如果使用)。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句