使用CI框架怎么實(shí)現(xiàn)一個(gè)圖片上傳功能-創(chuàng)新互聯(lián)

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

成都創(chuàng)新互聯(lián)公司專注于上街企業(yè)網(wǎng)站建設(shè),響應(yīng)式網(wǎng)站,成都商城網(wǎng)站開發(fā)。上街網(wǎng)站建設(shè)公司,為上街等地區(qū)提供建站服務(wù)。全流程按需規(guī)劃網(wǎng)站,專業(yè)設(shè)計(jì),全程項(xiàng)目跟蹤,成都創(chuàng)新互聯(lián)公司專業(yè)和態(tài)度為您提供的服務(wù)

首先在“./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ù),下面我們來看看控制器,在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>
    <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)容是否對(duì)您有幫助呢?如果還想對(duì)相關(guān)知識(shí)有進(jìn)一步的了解或閱讀更多相關(guān)文章,請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝您對(duì)創(chuàng)新互聯(lián)網(wǎng)站建設(shè)公司,的支持。

當(dāng)前文章:使用CI框架怎么實(shí)現(xiàn)一個(gè)圖片上傳功能-創(chuàng)新互聯(lián)
文章URL:http://muchs.cn/article0/dhecio.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供面包屑導(dǎo)航、商城網(wǎng)站、小程序開發(fā)、品牌網(wǎng)站設(shè)計(jì)、ChatGPT網(wǎng)站導(dǎo)航

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

手機(jī)網(wǎng)站建設(shè)