程序员最近都爱上了这个网站  程序员们快来瞅瞅吧!  it98k网:it98k.com

本站消息

站长简介/公众号

  出租广告位,需要合作请联系站长

+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

tp5下的文件上传与下载类

发布于2021-03-14 06:05     阅读(1356)     评论(0)     点赞(17)     收藏(1)


 1 class FieldInterfun extends Controller
 2 {
 3     /**
 4      * [upload 上传文件]
 5      * @param  [type]  $file [description]
 6      * @param  string  $name [指定文件夹]
 7      * @return [type]        [description]
 8      */
 9     public function upload($file,$name='img'){
10         $data=['code'=>2,'msg'=>'','path'=>''];
11         $movefile="upload/".$name;
12         // 移动到框架应用根目录/public/uploads/ 目录下
13         if($file && count($file)==1){
14             $info = $file->move($movefile);
15             if($info){
16                 $data['code']=1;
17                 $data['msg']='上传成功';
18                 $path='\\'.$movefile.DS.$info->getSaveName();
19                 $path       = strtr($path,'\\','/');
20                 $data['path']=$path;
21                 $ext=$info->getExtension();
22                 $getInfo=$info->getInfo();
23                 $data['file_type']=$getInfo['type'];
24                 // $file_typeD=explode('/', $getInfo['type']);
25                 // $data['file_type']=$file_typeD[0]=='application'?'file':$file_typeD[0];
26                 // if($ext=='zip')
27                 // {
28                 //     $data['file_type']='zip';
29                 // }
30                 // 
31                 $data['ext']=$ext;
32             }else{
33                 echo '<pre>';
34                 var_dump($file);
35                 $data['msg']=$file->getError();
36             }
37         }else{
38             
39         }
40         return $data;
41     }
42 
43 
44     /**
45      * [download 文件下载]
46      * @param  [type] $file_url [description]
47      * @param  string $new_name [description]
48      * @return [type]           [description]
49      */
50     public function download($file_url,$new_name=''){
51         if(!isset($file_url)||trim($file_url)==''){
52           echo '500';
53         }
54         if(!file_exists($file_url)){ //检查文件是否存在
55           echo '404';
56         }
57         $file_name=basename($file_url);
58         $file_type=explode('.',$file_url);
59         $file_type=$file_type[count($file_type)-1];
60         $file_name=trim($new_name=='')?$file_name:urlencode($new_name);
61         $file_type=fopen($file_url,'r'); //打开文件
62         //输入文件标签
63         header("Content-type: application/octet-stream");
64         header("Accept-Ranges: bytes");
65         header("Accept-Length: ".filesize($file_url));
66         header("Content-Disposition: attachment; filename=".$file_name);
67         //输出文件内容
68         echo fread($file_type,filesize($file_url));
69         fclose($file_type);
70     }
71 
72 
73     
74 }

 



所属网站分类: 技术文章 > 博客

作者:悲催的程序员

链接:http://www.phpheidong.com/blog/article/3218/baf15f6ef01c5c487736/

来源:php黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

17 0
收藏该文
已收藏

评论内容:(最多支持255个字符)