使用CI框架怎么實現(xiàn)一個圖片上傳功能?很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來學(xué)習(xí)下,希望你能有所收獲。

首先在“./application/views/”文件夾下創(chuàng)一個視圖文件: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中一個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)建另外一個文件upload_success.php
<html> <head> <title>Upload Form</title> </head> <body> <h4>Your file was successfully uploaded!</h4> <ul> <?php <foreach($upload_data as $item=>$value):?> <li> <?php echo $item;?>:<?php echo $value;?> </li> <?php?> </ul> </body> </html>
看完上述內(nèi)容是否對您有幫助呢?如果還想對相關(guān)知識有進(jìn)一步的了解或閱讀更多相關(guān)文章,請關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝您對創(chuàng)新互聯(lián)網(wǎng)站建設(shè)公司,的支持。
當(dāng)前文章:使用CI框架怎么實現(xiàn)一個圖片上傳功能-創(chuàng)新互聯(lián)
文章URL:http://www.chinadenli.net/article0/dhecio.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供面包屑導(dǎo)航、商城網(wǎng)站、小程序開發(fā)、品牌網(wǎng)站設(shè)計、ChatGPT、網(wǎng)站導(dǎo)航
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容