CI(CodeIgniter)框架實(shí)現(xiàn)圖片上傳的方法
本文實(shí)例講述了CodeIgniter框架實(shí)現(xiàn)圖片上傳的方法。分享給大家供大家參考,具體如下:
對于圖片上傳這種老生常談的問題,在此我不得不再次重復(fù)一次,因?yàn)閷τ谶@框架畢竟有些地方值得自己學(xué)習(xí)與借鑒,這篇文章我是借助官方文檔來寫的,但有些地方任然需要標(biāo)明一下。
下面我們來看看圖片上傳吧。首先在“./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類庫,下面我們來看看控制器,在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>
更多關(guān)于CodeIgniter相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《codeigniter入門教程》、《CI(CodeIgniter)框架進(jìn)階教程》、《php優(yōu)秀開發(fā)框架總結(jié)》、《ThinkPHP入門教程》、《ThinkPHP常用方法總結(jié)》、《Zend FrameWork框架入門教程》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對大家基于CodeIgniter框架的PHP程序設(shè)計(jì)有所幫助。
相關(guān)文章
CI框架實(shí)現(xiàn)優(yōu)化文件上傳及多文件上傳的方法
這篇文章主要介紹了CI框架實(shí)現(xiàn)優(yōu)化文件上傳及多文件上傳的方法,結(jié)合實(shí)例形式詳細(xì)分析了CI框架優(yōu)化文件上傳及多文件上傳的實(shí)現(xiàn)思路與具體操作步驟,需要的朋友可以參考下2017-01-01
PHP讀取Excel內(nèi)的圖片(phpspreadsheet和PHPExcel擴(kuò)展庫)
解決php用mysql方式連接數(shù)據(jù)庫出現(xiàn)Deprecated報(bào)錯(cuò)問題
阿里云服務(wù)器搭建Php+Apache運(yùn)行環(huán)境的詳細(xì)過程

