Front-end code
<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>
Controller:
Define an array and set some upload-related parameters
$config['upload_path'] = './uploads/'; //设置允许上传的类型 $config['allowed_types'] = 'gif|jpg|png'; $config['max_size'] = '100'; //如果是图片还可以设置最大高度和宽度 $config['max_height'] = 768; $config['max_width'] = 1024;
Call CI’s upload general class and perform upload
//upload为调用的类名,全小写 $this->load->library('upload',$config); //如果上传框的name写的是userfile,那就不用传参数了,如果不是,把name的值传进去 $this->upload->do_upload('上传框的name');
Receive error message or success message
//出错信息 $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); } } }
Front-end code
<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>
Controller:
Define an array and set some upload-related parameters
$config['upload_path'] = './uploads/'; //设置允许上传的类型 $config['allowed_types'] = 'gif|jpg|png'; $config['max_size'] = '100'; //如果是图片还可以设置最大高度和宽度 $config['max_height'] = 768; $config['max_width'] = 1024;
Call CI's upload general class and execute the upload
//upload为调用的类名,全小写 $this->load->library('upload',$config); //如果上传框的name写的是userfile,那就不用传参数了,如果不是,把name的值传进去 $this->upload->do_upload('上传框的name');
Receive error or success information
//出错信息 $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); } } }
For more image uploading in the ci framework, please pay attention to the PHP Chinese website for related articles !