欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

ThinkPHP文件上傳實(shí)例教程

 更新時(shí)間:2014年08月22日 15:26:05   投稿:shichen2014  
這篇文章主要介紹了ThinkPHP文件上傳實(shí)現(xiàn)方法,是ThinkPHP程序開發(fā)中非常常見(jiàn)的一個(gè)技巧,需要的朋友可以參考下

文件上傳是很多PHP程序項(xiàng)目中常見(jiàn)的一個(gè)功能,今天本文就來(lái)分享一個(gè)完整的實(shí)例,來(lái)實(shí)現(xiàn)ThinkPHP文件上傳的功能。具體方法如下:

一、action部分:

FileAction.class.php頁(yè)面代碼如下:

<?php
class FileAction extends Action{
  function index(){
    $file=M('file');
    $list=$file->select();
    $this->assign('filelist',$list);
    $this->display();  
  }  
  function upload(){
    //文件上傳地址提交給他,并且上傳完成之后返回一個(gè)信息,讓其寫入數(shù)據(jù)庫(kù)  
    if(empty($_FILES)){
      $this->error('必須選擇上傳文件');  
    }else{
      $a=$this->up();
      if(isset($a)){
        //寫入數(shù)據(jù)庫(kù)的自定義c方法
        if($this->c($a)){
          $this->success('上傳成功');  
        }
        else{
          $this->error('寫入數(shù)據(jù)庫(kù)失敗');  
        }
      }else{
        $this-error('上傳文件異常,請(qǐng)與系統(tǒng)管理員聯(lián)系');  
      }
    }
  }
  private function c($data){
    $file=M('file');
    $num  =  '0';
    for($i = 0; $i < count($data)-1; $i++) {
      $data['filename']=$data[$i]['savename'];      
      if( $file->data($data)->add())
      {
        $num++;
      }
    }
    if($num==count($data)-1)
    {
      return true;  
    }else
    {
      return false;
    }
  }
  private function up(){
    //完成與thinkphp相關(guān)的,文件上傳類的調(diào)用  
    import('@.Org.UploadFile');//將上傳類UploadFile.class.php拷到Lib/Org文件夾下
    $upload=new UploadFile();
    $upload->maxSize='1000000';//默認(rèn)為-1,不限制上傳大小
    $upload->savePath='./Public/Upload/';//保存路徑建議與主文件平級(jí)目錄或者平級(jí)目錄的子目錄來(lái)保存  
    $upload->saveRule=uniqid;//上傳文件的文件名保存規(guī)則
    $upload->uploadReplace=true;//如果存在同名文件是否進(jìn)行覆蓋
    $upload->allowExts=array('jpg','jpeg','png','gif');//準(zhǔn)許上傳的文件類型
    $upload->allowTypes=array('image/png','image/jpg','image/jpeg','image/gif');//檢測(cè)mime類型
    $upload->thumb=true;//是否開啟圖片文件縮略圖
    $upload->thumbMaxWidth='300,500';
    $upload->thumbMaxHeight='200,400';
    $upload->thumbPrefix='s_,m_';//縮略圖文件前綴
    $upload->thumbRemoveOrigin=1;//如果生成縮略圖,是否刪除原圖
    
    if($upload->upload()){
      $info=$upload->getUploadFileInfo();
      return $info;
    }else{
      $this->error($upload->getErrorMsg());//專門用來(lái)獲取上傳的錯(cuò)誤信息的  
    }  
  }
}
?>

二、view模板部分:

模板文件index.html代碼如下:

<html>
<body>
<volist name="filelist" id="vo">
 小圖:<img src="__PUBLIC__/upload/s_{$vo['filename']}" /><br />
 大圖:<img src="__PUBLIC__/upload/m_{$vo['filename']}" /><br />
</volist>
<form action="__URL__/upload" method="post" enctype="multipart/form-data">
  <input type="file" name="file[]" /><br />
  <input type="file" name="file[]" /><br />
  <input type="file" name="file[]" /><br />
  <input type="submit" value="上傳" />
</form>

</body>
</html>

相信本文所述實(shí)例對(duì)大家的ThinkPHP程序開發(fā)可以起到一定的借鑒作用。

相關(guān)文章

最新評(píng)論