php简单文件上传下载,完成简单的文件上传/下载功能的页面

摘要:html>

&nbhtml>

content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">

文件上传/下载页面

请选择需要上传的文件:   

 请确定开始上传此文件:  

//upFile.php

require 'upFun.php';

print_r(upFile($_FILES['upFile']));

?>//down.php

require 'upFun.php';

$downFile=$_GET['downFileName'];

dowFile($downFile);

?>//upFun.php

function dowFile($fileName)

{

header('Accept-Length:'.filesize($fileName));

//  主要是这一行

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

readfile($fileName);

}

function upFile($fileInfo,$uploadPath='./upload',$allowExt = ['png','jpg','jpeg','gif'

,'txt','html'],$maxSize = 2000000){

if($fileInfo['error'] ===0){

//返回文件后缀名

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

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

return '非法文件类型';

}

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

return '非法上传!';

}

if(!is_dir($uploadPath)){

mkdir($uploadPath,0777,true);

}

//文件名,用md生成的

$uniName = md5(uniqid(microtime(true),true)).".".$ext;

//新文件名

$dest = $uploadPath."/".$uniName;

if(!move_uploaded_file($fileInfo['tmp_name'],$dest))

{

return '文件上传失败';

}

return '文件上传成功';

}else{

switch ($fileInfo['error']){

case 1:

$res = '上传的文件超过了php.ini 中

upload_max_filesize 选项限制的值';

break;

case 2:

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

break;

case 3:

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

break;

case 4:

$res ='没有文件被上传';

break;

case 6:

case 7:

$res = '系统错误';

break;

}

return $res;

}

}

46a8c03327c838c6f86a9a85cd9ee630.png

批改老师:欧阳批改时间:2019-04-18 09:53:49

老师总结:完成的不错,上传文件格式记得要限制一下。不然会有病毒上传。继续加油。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值