本文實(shí)例講述了CodeIgniter框架實(shí)現(xiàn)圖片上傳的方法。分享給大家供大家參考,具體如下:
對(duì)于圖片上傳這種老生常談的問題,在此我不得不再次重復(fù)一次,因?yàn)閷?duì)于這框架畢竟有些地方值得自己學(xué)習(xí)與借鑒,這篇文章我是借助官方文檔來(lái)寫的,但有些地方任然需要標(biāo)明一下。
下面我們來(lái)看看圖片上傳吧。首先在“./application/views/”文件夾下創(chuàng)一個(gè)視圖文件:text.php,代碼如下:
<html>  <head>    <title>Upload Form</title>  </head>  <body>      <?php echo $error;?>      <?php echo form_open_multipart('upload/do_upload');?>      <input type="file" name="userfile" size="20"/>      <br><br>      <input type="submit" value="upload"/>      </form>  </body></html>Codeigniter有自己非常豐富upload類庫(kù),下面我們來(lái)看看控制器,在Controller中一個(gè)Upload.php文件,代碼如下:
class Upload extends CI_Controller{  public function __construct(){    parent::__construct();    $this->load->helper("form","url");  }  public function index(){    $this->load->view('test',array("error"=>''));  }  public function do_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('userfile')){      $error=array('error'=>$this->upload->display_errors());      $this->load->view('test',$error);    }else{      $data=array('upload_data'=>$this->upload->data());      $this->load->view('upload_success',$data);    }  }}下面在視圖中創(chuàng)建另外一個(gè)文件upload_success.php
<html> <head> <title>Upload Form</title> </head> <body> <h3>Your file was successfully uploaded!</h3> <ul> <?php <foreach($upload_data as $item=>$value):?> <li> <?php echo $item;?>:<?php echo $value;?> </li> <?php?> </ul> </body></html>
希望本文所述對(duì)大家基于CodeIgniter框架的PHP程序設(shè)計(jì)有所幫助。
新聞熱點(diǎn)
疑難解答
圖片精選