ci框架中的图片上传
程序员文章站
2022-04-18 12:12:43
...
前端代码
<html> <form action="ci/CodeIgniter_2.2.0/index.php/upload/up" method="post" enctype="multipart/form-data"> <input type="file" name="upfile" /> <input type="submit" name="sub" value="提交" /> </form> </html>
控制器:
定义一个数组,设置一些与上传相关的参数
$config['upload_path'] = './uploads/'; //设置允许上传的类型 $config['allowed_types'] = 'gif|jpg|png'; $config['max_size'] = '100'; //如果是图片还可以设置最大高度和宽度 $config['max_height'] = 768; $config['max_width'] = 1024;
调用CI的上传通用类,并执行上传
//upload为调用的类名,全小写 $this->load->library('upload',$config); //如果上传框的name写的是userfile,那就不用传参数了,如果不是,把name的值传进去 $this->upload->do_upload('上传框的name');
接收出错信息或成功信息
//出错信息 $error = array('error' => $this->upload->display_error()); //成功信息 $data = array('upload_data' => $this->upload->data());
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); class Upload extends CI_Controller { //显示带表单的视图 public function index(){ $this->load->view('up'); } //显示上传信息 public function up(){ $config['upload_path'] = './uploads/'; $config['allowed_types'] = 'gif|jpg|png'; $config['max_size'] = "2000"; $this->load->library('upload',$config); //打印成功或错误的信息 if($this->upload->do_upload('upfile')) { $data = array("upload_data" => $this->upload->data()); var_dump($data); } else { $error = array("error" => $this->upload->display_errors()); var_dump($error); } } }
前端代码
<html> <form action="ci/CodeIgniter_2.2.0/index.php/upload/up" method="post" enctype="multipart/form-data"> <input type="file" name="upfile" /> <input type="submit" name="sub" value="提交" /> </form> </html>
控制器:
定义一个数组,设置一些与上传相关的参数
$config['upload_path'] = './uploads/'; //设置允许上传的类型 $config['allowed_types'] = 'gif|jpg|png'; $config['max_size'] = '100'; //如果是图片还可以设置最大高度和宽度 $config['max_height'] = 768; $config['max_width'] = 1024;
调用CI的上传通用类,并执行上传
//upload为调用的类名,全小写 $this->load->library('upload',$config); //如果上传框的name写的是userfile,那就不用传参数了,如果不是,把name的值传进去 $this->upload->do_upload('上传框的name');
接收出错信息或成功信息
//出错信息 $error = array('error' => $this->upload->display_error()); //成功信息 $data = array('upload_data' => $this->upload->data());
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); class Upload extends CI_Controller { //显示带表单的视图 public function index(){ $this->load->view('up'); } //显示上传信息 public function up(){ $config['upload_path'] = './uploads/'; $config['allowed_types'] = 'gif|jpg|png'; $config['max_size'] = "2000"; $this->load->library('upload',$config); //打印成功或错误的信息 if($this->upload->do_upload('upfile')) { $data = array("upload_data" => $this->upload->data()); var_dump($data); } else { $error = array("error" => $this->upload->display_errors()); var_dump($error); } } }
更多ci框架中的图片上传 相关文章请关注PHP中文网!