生活随笔
收集整理的這篇文章主要介紹了
实例讲解ThinkPHP的UploadFile文件上传类的详细用法
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
ThinkPHP文件上傳自帶了上傳類,使用起來(lái)非常方便,我們將以一個(gè)文件上傳實(shí)例來(lái)講解ThinkPHP上傳類的實(shí)際用法,上傳類使用時(shí)有詳細(xì)的中文注釋,可以非常方便的告訴你類的一些方法或變量的使用方法。
http://www.itokit.com/2012/1115/74820.html
FileAction.class.php
PHP Code復(fù)制內(nèi)容到剪貼板 1 <?php
2 class FileAction extends Action{
3 function index(){
4 $file=M('file');
5 $list=$file->select();
6 $this->assign('filelist',$list);
7 $this->display();
8 }
9
10 function upload(){
11 //文件上傳地址提交給他,并且上傳完成之后返回一個(gè)信息,讓其寫入數(shù)據(jù)庫(kù)
12 if(emptyempty($_FILES)){
13 $this->error('必須選擇上傳文件');
14 }else{
15 $a=$this->up();
16 if(isset($a)){
17 //寫入數(shù)據(jù)庫(kù)的自定義c方法
18 if($this->c($a)){
19 $this->success('上傳成功');
20 }
21 else{
22 $this->error('寫入數(shù)據(jù)庫(kù)失敗');
23 }
24 }else{
25 $this-error('上傳文件異常,請(qǐng)與系統(tǒng)管理員聯(lián)系');
26 }
27 }
28 }
29
30 private function c($data){
31 $file=M('file');
32 $num = '0';
33 for($i = 0; $i < count($data)-1; $i++) {
34 $data['filename']=$data[$i]['savename'];
35 if( $file->data($data)->add())
36 {
37 $num++;
38 }
39 }
40 if($num==count($data)-1)
41 {
42 return true;
43 }else
44 {
45 return false;
46 }
47
48 }
49
50 private function up(){
51 //完成與thinkphp相關(guān)的,文件上傳類的調(diào)用
52 import('@.Org.UploadFile');//將上傳類UploadFile.class.php拷到Lib/Org文件夾下
53 $upload=new UploadFile();
54 $upload->maxSize='1000000';//默認(rèn)為-1,不限制上傳大小
55 $upload->savePath='./Public/Upload/';//保存路徑建議與主文件平級(jí)目錄或者平級(jí)目錄的子目錄來(lái)保存
56 $upload->saveRule=uniqid;//上傳文件的文件名保存規(guī)則
57 $upload->uploadReplace=true;//如果存在同名文件是否進(jìn)行覆蓋
58 $upload->allowExts=array('jpg','jpeg','png','gif');//準(zhǔn)許上傳的文件類型
59 $upload->allowTypes=array('image/png','image/jpg','image/jpeg','image/gif');//檢測(cè)mime類型
60 $upload->thumb=true;//是否開啟圖片文件縮略圖
61 $upload->thumbMaxWidth='300,500';
62 $upload->thumbMaxHeight='200,400';
63 $upload->thumbPrefix='s_,m_';//縮略圖文件前綴
64 $upload->thumbRemoveOrigin=1;//如果生成縮略圖,是否刪除原圖
65
66 if($upload->upload()){
67 $info=$upload->getUploadFileInfo();
68 return $info;
69 }else{
70 $this->error($upload->getErrorMsg());//專門用來(lái)獲取上傳的錯(cuò)誤信息的
71 }
72 }
73
74 }
75 ?>
模板文件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的相關(guān)推薦文章:
1、ThinkPHP U函數(shù)生成URL偽靜態(tài)(自動(dòng)生成相應(yīng)的URL地址)
2、使用PHP的框架ThinkPHP必須要掌握的程序開發(fā)調(diào)試手段
3、thinkphp學(xué)習(xí)筆記入門小知識(shí)點(diǎn)分享
轉(zhuǎn)載于:https://www.cnblogs.com/gzmg/p/3431137.html
總結(jié)
以上是生活随笔為你收集整理的实例讲解ThinkPHP的UploadFile文件上传类的详细用法的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。