文件的上传uploadfile、下载downFile函数

原创2019-09-04 07:34:0711
摘要:<?php//文件的上传 function uploadfile($fileinfo,$uploadpath='upload',$allowExt=['png','jpg','jpeg','gif','txt','html'],$maxsize=2000000,$reslut=&#

<?php

//文件的上传

function uploadfile($fileinfo,$uploadpath='upload',$allowExt=['png','jpg','jpeg','gif','txt','html'],$maxsize=2000000,$reslut=''){

             switch($fileinfo['error']){

                                 case 0 :

                                             //获取文件后缀

                                             $ext=strtolower(pathinfo($fileinfo['name'],PATHINFO_EXTENSION));

                                             //判断后缀格式

                                             if(!in_array($ext,$allowExt)){

                                             return '文件类型非法上传';

                                             }

                                             //判断文件是否超过容量

                                             if($fileinfo['size']>$maxsize){

                                             return '文件超过容量';

                                             }

                                             //上传目录是否存在

                                             if(!is_dir($uploadpath)){

                                             mkdir($uploadpath,0777,true);

                                             }

                                             //指定上传文件名

                                             $newfile=md5(uniqid(time(true),true)).'.'.$ext;

                                             //上传文件是否为POST方式

                                             if(!is_uploaded_file($fileinfo['tmp_name'])){

                                             return '文件非法上传';

                                             }

                                             //上传文件

                                             if(!move_uploaded_file($fileinfo['tmp_name'], $uploadpath.'/'.$newfile)){

                                             return '文件上传失败!';

                                             }

                                 break;

                                 case 1 :

                                 $reslut='上传的文件超过了 php.ini 中 upload_max_filesize 选项限制的值。';

                                 break;

                                 case 2 :

                                 $reslut='上传文件的大小超过了 HTML 表单中 MAX_FILE_SIZE 选项指定的值。';

                                 break;

                                 case 3 :

                                 $reslut='文件只有部分被上传。';

                                 break;

                                 case 4 :

                                 $reslut='没有文件被上传。';

                                 break;

                                 case 6 :

                                 $reslut='找不到临时文件夹。';

                                 break;

                                 case 7 :

                                 $reslut='文件写入失败。';

                                 break;

                                 default :

                                 $reslut='不知明错误。';

                                 }

                         if(!$reslut==''){

                         return $reslut;

                         }

               return '文件上传成功';

}

//文件下载

function downFile($filename){

             header('Accept-Lenght:'.filesize($filename));

             header('Content-Disposition:attachment;filename='.basename($filename));

             readfile($filename);

}


批改老师:欧阳克批改时间:2019-09-05 14:36:12
老师总结:完成的不错。思路很清晰,后缀名和文件大小,一定要判断。继续加油

发布手记

热门词条