防止在 codeigniter 的图片上传功能中选择多张图片和 Select 仅选择单张图片
Prevent selecting multiple images and Select only single image in image upload function in codeigniter
我想停止 select 多张图片和 select 单张图片。
在这个例子中,我在 codeigniter 中上传图片,我可以 select 多张图片,但我必须 select 只有单张图片。那么如何将 selection 更改为 select 单张图像。
在这个例子中,当我们编辑上传的图像时,旧图像将从数据库中删除我想改变它,但我不知道在哪里以及如何改变它的代码。
这是我的控制器。
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');
}
}
I want to stop selecting multiple Images and select only single image.
这似乎在您文件的视图部分。
如果我是对的,解决方案是将 <input type="file" name="userfile" multiple>
更改为 <input type="file" name="userfile">
。
这应该可以解决您的多图选择问题。
简而言之,如果使用 multiple
属性,请从文件输入中删除它。
我想停止 select 多张图片和 select 单张图片。 在这个例子中,我在 codeigniter 中上传图片,我可以 select 多张图片,但我必须 select 只有单张图片。那么如何将 selection 更改为 select 单张图像。 在这个例子中,当我们编辑上传的图像时,旧图像将从数据库中删除我想改变它,但我不知道在哪里以及如何改变它的代码。
这是我的控制器。
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');
}
}
I want to stop selecting multiple Images and select only single image.
这似乎在您文件的视图部分。
如果我是对的,解决方案是将 <input type="file" name="userfile" multiple>
更改为 <input type="file" name="userfile">
。
这应该可以解决您的多图选择问题。
简而言之,如果使用 multiple
属性,请从文件输入中删除它。