ci框架(codeigniter)实现多文件上传方法
1、优化文件上传方法
Codeigniter手册里面的那种大家常用的方法在这里就不重复描述了,下面直接说如何对方法进行优化以达到降低代码冗余,提高代码重复利用率的目的。
a) 首先在 “ application/config ” 新建 " upload.php " 配置文件
在 “ application/config ” 新建 " upload.php" 配置文件,在里面写入上传的配置参数。
<?php defined('BASEPATH') OR exit('No direct script access allowed'); //上传的参数配置 $config['upload_path'] = './public/'; $config['allowed_types'] = 'gif|png|jpg'; $config['max_size'] = 100; $config['max_width'] = '1024'; $config['max_height'] = '768'; |
注意:upload_path参数所代表的路径文件夹你已经在项目中创建完毕!
b) 在控制器的构造函数中加载文件上传类
<?php defined('BASEPATH') OR exit('No direct script access allowed'); /** * 控制器 */ class Brand extends Admin_Controller { public function __construct() { parent::__construct(); $this->load->model('brand_model'); $this->load->library('form_validation'); //激活分析器以调试程序 $this->output->enable_profiler(TRUE); //配置中上传的相关参数会自动加载 $this->load->library('upload'); } } |
注意:我们在第一步创建的 “ upload.php ” 文件中的上传配置信息会在这里会自动进行加载。
c) 编写上传方法执行do_upload()方法进行文件上传
public function insert() { //设置验证规则 $this->form_validation->set_rules('brand_name','名称','required'); if($this->form_validation->run() == false){ //未通过验证 $data['message'] = validation_errors(); $data['wait'] = 3; $data['url'] = site_url('admin/brand/add'); $this->load->view('message.html',$data); }else{ //通过验证,处理图片上传 if ($this->upload->do_upload('logo')) { //logo为前端file控件名 //上传成功,获取文件名 $fileInfo = $this->upload->data(); $data['logo'] = $fileInfo['file_name']; //获取表单提交数据 $data['brand_name'] = $this->input->post('brand_name'); $data['url'] = $this->input->post('url'); $data['brand_desc'] = $this->input->post('brand_desc'); $data['sort_order'] = $this->input->post('sort_order'); $data['is_show'] = $this->input->post('is_show'); //调用模型完成添加动作 if($this->brand_model->add_brand($data)){ $data['message'] = "添加成功"; $data['wait'] = 3; $data['url'] = site_url('admin/brand/index'); $this->load->view('message.html',$data); }else{ $data['message'] = "添加失败"; $data['wait'] = 3; $data['url'] = site_url('admin/brand/add'); $this->load->view('message.html',$data); } }else{ //上传失败 $data['message'] = $this->upload->display_errors(); $data['wait'] = 3; $data['url'] = site_url('admin/brand/add'); $this->load->view('message.html',$data); } } } |
注意:上述代码有部分是我项目中的代码,大家可以忽略直接关注关键的上传代码。当你需要上传不同的文件时,你也可以在方法中进行文件上传配置,使用$this->upload->initialize()方法进行配置。
2、同时上传多文件的两种方法
① 方法一思路:对所上传的多个文件进行循环处理
/** * Codeigniter框架实现多文件上传 * @author Zhihua_W * 方法一:对上传的文件进行循环处理 */ public function multiple_uploads1() { //载入所需文件上传类库 $this->load->library('upload'); //配置上传参数 $upload_config = array( 'upload_path' => './public/', 'allowed_types' => 'jpg|png|gif', 'max_size' => '500', 'max_width' => '1024', 'max_height' => '768', ); $this->upload->initialize($upload_config); //循环处理上传文件 foreach ($_FILES as $key => $value) { if (!empty($key['name'])) { if ($this->upload->do_upload($key)) { //上传成功 print_r($this->upload->data()); } else { //上传失败 echo $this->upload->display_errors(); } } } } |
② 方法二思路:直接一下将多个文件全部上传然后在对上传过的数据进行处理
/** * Codeigniter框架实现多文件上传 * @author Zhihua_W * 方法二:直接一下将多个文件全部上传然后在对上传过的数据进行处理 */ public function multiple_uploads2() { $config['upload_path'] = './public/'; //这里的public是相对于index.php的,也就是入口文件,这个千万不能弄错! //否则就会报错:"The upload path does not appear to be valid."; $config['allowed_types'] = 'gif|jpg|png'; //我试着去上传其它类型的文件,这里一定要注意顺序! //否则报错:"A problem was encountered while attempting to move the uploaded file to the final destination." //这个错误一般是上传文件的文件名不能是中文名,这个很郁闷!还未解决,大家可以用其它方法,重新改一下文件名就可以解决了! //$config['allowed_types'] = 'zip|gz|png|gif|jpg';(正确) //$config['allowed_types'] = 'png|gif|jpg|zip|gz';(错误) $config['max_size'] = '1024'; $config['max_width'] = '1024'; $config['max_height'] = '768'; $config['file_name'] = time(); //文件名不使用原始名 $this->load->library('upload', $config); if (!$this->upload->do_upload()) { echo $this->upload->display_errors(); } else { $data['upload_data'] = $this->upload->data(); //上传文件的一些信息 $img = $data['upload_data']['file_name']; //取得文件名 echo $img . "<br>"; foreach ($data['upload_data'] as $item => $value) { echo $item . ":" . $value . "<br>"; } } } |