如何在codeigniter中上传2个单独的图像
Posted
技术标签:
【中文标题】如何在codeigniter中上传2个单独的图像【英文标题】:How to upload 2 separate images in codeigniter 【发布时间】:2016-07-27 21:22:39 【问题描述】:我想分别上传 2 张图片。我有一个条件,我需要上传文件,有时需要上传文件,有时只需要图像,我需要使用单独的按钮来区分图像和文件。
我的代码是
<form action="http://localhost/cod_login/club/test2" enctype="multipart/form-data" method="post" accept-charset="utf-8">
<input type="file" name="userfile" size="20">
<input type="file" name="userfile" size="20">
<input type="submit" name="submit" value="upload">
</form>
这是控制器
function ddoo_upload()
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '100';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload())
$error = array('error' => $this->upload->display_errors());
$this->load->view('upload_form', $error);
else
$data = array('upload_data' => $this->upload->data());
$this->load->view('upload_success', $data);
【问题讨论】:
可以只使用一个文件上传按钮,在按钮中添加multiple="multiple"
属性,区分服务器端的文件类型。
不,我需要两个按钮,而不是我们客户要求的一个
你可以看看这个answer,根据文件类型上传。
【参考方案1】:
如果你想要两个不同的文件按钮,你需要给他们不同的名字。
<form action="" enctype="multipart/form-data" method="post" accept-charset="utf-8">
<input type="file" name="userfile1" size="20">
<input type="file" name="userfile2" size="20">
<input type="submit" name="submit" value="upload">
你必须修改你的函数ddoo_upload()
,如下所示
:-
function ddoo_upload($filename)
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '100';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload($filename))
$error = array('error' => $this->upload->display_errors());
return false;
// $this->load->view('upload_form', $error);
else
$data = array('upload_data' => $this->upload->data());
return true;
//$this->load->view('upload_success', $data);
注意:-我们将$filename
作为变量传递,而不是使用它来上传不同的文件。
现在在表单动作重定向的控制器中,您需要编写以下代码。
if ($this->input->post('submit'))
if (isset($_FILES['userfile1']) && $_FILES['userfile1']['name'] != '')
$file1 = $this->ddoo_upload('userfile1');
if (isset($_FILES['userfile2']) && $_FILES['userfile2']['name'] != '')
$file2 = $this->ddoo_upload('userfile2');
【讨论】:
【参考方案2】:<form action="http://localhost/cod_login/club/test2" enctype="multipart/form-data" method="post" accept-charset="utf-8">
<input type="file" name="userfile" size="20" multiple="">
<input type="submit" name="submit" value="upload">
</form>
【讨论】:
先生,我需要两个按钮而不是一个 需要单独上传图片 一键上传单张图片,其他多张图片..对吧?【参考方案3】:这是我在codeigniter中申请上传两张图片的控制器代码
公共函数索引()
if($this->input->post('Submit'))
//-----------Image File Section Start Here -----------//
$config['upload_path'] = './uploads/'; // Directory
$config['allowed_types'] = 'jpg|jpeg|bmp|png'; //type of images allowed
$config['max_size'] = '30720'; //Max Size
$config['encrypt_name'] = TRUE; // For unique image name at a time
$this->load->library('upload', $config); //File Uploading library
$this->upload->do_upload('userfile'); // input name which have to upload
$video_upload=$this->upload->data(); //variable which store the path
//--------------End of Image File Section------------------------//
//---------Thumbnail Image Upload Section Start Here -----------//
$config2['upload_path'] = './thumb/'; // Directory
$config2['allowed_types'] = 'jpg|jpeg|bmp|png'; //type of images allowed
$config2['max_size'] = '30720'; //Max Size
$config2['encrypt_name'] = TRUE; // For unique image name at a time
$this->upload->initialize($config2); //we can not use upload library again and again it will not initialize again and again so thats why i have used initialize
$this->upload->do_upload('txt_thumb'); // File Name
$thumbnail_upload=$this->upload->data(); // store the name of the file
//--------End of Thumbnail Upload Section-----------//
$date=date("d-m-Y"); // Store current date in variable
// Here the database query to insert
$data = array(
'parent_id'=> $this->input->post('txt_parent'),
'cat_id' => $this->input->post('txt_category'),
'title'=> $this->input->post('txt_title'),
'status' => $this->input->post('txt_status'),
'featured' => $thumbnail_upload['file_name'],
'image' => $video_upload['file_name'],
'time'=>$date
);
$sql_ins= $this->Insimage->insertimage($data);
if($sql_ins)
$data['Success'] = "Image has been succesfully inserted!!";
此代码肯定可以上传 2 张图片 享受!!!! :-)
【讨论】:
【参考方案4】:1- 在你的控制器中创建 image_uploader
function image_uploader($filename)
$config['upload_path'] = './assets/uploads/setting/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '100';
$config['max_width'] = '2000';
$config['max_height'] = '2000';
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload($filename))
$error = array('error' => $this->upload->display_errors());
else
$data_foto = $this->upload->data();
return $data_foto['file_name'];
如果上传成功,此函数返回图片文件名
2- 在其他函数中调用 image_uploader(视图中的表单操作)
public function slideroneupload()
$this->site_security->_make_sure_is_admin();
$data = $this->input->post();
$sliderimage = $this->image_uploader('image');
$thumbimage = $this->image_uploader('thumb');
// save all data to database
.
.
.
【讨论】:
以上是关于如何在codeigniter中上传2个单独的图像的主要内容,如果未能解决你的问题,请参考以下文章
如何在 [codeigniter] 中上传具有随机名称规则的图像