ThinkPHP檔案上傳執行個體

來源:互聯網
上載者:User
這篇文章主要介紹了ThinkPHP檔案上傳實現方法,是ThinkPHP程式開發中非常常見的一個技巧,需要的朋友可以參考下

檔案上傳是很多PHP程式項目中常見的一個功能,今天本文就來分享一個完整的執行個體,來實現ThinkPHP檔案上傳的功能。具體方法如下:

一、action部分:

FileAction.class.php頁面代碼如下:

<?phpclass FileAction extends Action{  function index(){    $file=M('file');    $list=$file->select();    $this->assign('filelist',$list);    $this->display();    }    function upload(){    //檔案上傳地址提交給他,並且上傳完成之後返回一個資訊,讓其寫入資料庫      if(empty($_FILES)){      $this->error('必須選擇上傳檔案');      }else{      $a=$this->up();      if(isset($a)){        //寫入資料庫的自訂c方法        if($this->c($a)){          $this->success('上傳成功');          }        else{          $this->error('寫入資料庫失敗');          }      }else{        $this-error('上傳檔案異常,請與系統管理員聯絡');        }    }  }  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相關的,檔案上傳類的調用      import('@.Org.UploadFile');//將上傳類UploadFile.class.php拷到Lib/Org檔案夾下    $upload=new UploadFile();    $upload->maxSize='1000000';//預設為-1,不限制上傳大小    $upload->savePath='./Public/Upload/';//儲存路徑建議與主檔案平級目錄或者平級目錄的子目錄來儲存      $upload->saveRule=uniqid;//上傳檔案的檔案名稱儲存規則    $upload->uploadReplace=true;//如果存在同名檔案是否進行覆蓋    $upload->allowExts=array('jpg','jpeg','png','gif');//准許上傳的檔案類型    $upload->allowTypes=array('image/png','image/jpg','image/jpeg','image/gif');//檢測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());//專門用來擷取上傳的錯誤資訊的      }    }}?>

二、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>

相信本文所述執行個體對大家的ThinkPHP程式開發可以起到一定的借鑒作用。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.