(1)fileUpload.html文件
<!doctype html>
<html lang="en">
<head><meta charset="UTF-8"><meta name="viewport"content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"><meta http-equiv="X-UA-Compatible" content="ie=edge"><title>Document</title>
</head>
<body>
<form action="upload.php" method="post" enctype="multipart/form-data"><input type="hidden" name="MAX_FILE_SIZE" value="2621114"><input type="file" required name="upload_file"><input type="submit" value="提交">
</form>
</body>
</html>
(2)upload.php文件
<?php
if (is_uploaded_file($_FILES['upload_file']['tmp_name'])){$upfile = $_FILES['upload_file'];//print_r($upfile);$name = $upfile['name'];//获取文件名$type = $upfile['type'];//文件类型$size = $upfile['size'];//文件大小$tmp_name = $upfile['tmp_name'];//服务器存储的临时名称$error = $upfile['error'];//错误信息/* switch ($type){case 'image/png':$ok=1;break;case 'image/jpg':$ok=1;break;case 'image/jif':$ok=1;break;case 'image/jpeg':$ok=1;break;$ok &&}*/if ( $error == 0){//保证文件路径存在(这里不会新建);move_uploaded_file($tmp_name,'./uploads/'.iconv("UTF-8", "gbk",$name));echo '文件上传成功';}else{echo '文件上传失败';}}
(1)fileDownLoad.html文件
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" ".dtd">
<html xmlns="">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>Untitled Document</title>
</head><body>
<a href="download.php?file=现金">下载</a>
</body>
</html>
(2)download.php文件
<?php
//download.php
//$upfile ="uploads\"." ".$_GET['file'];
//echo $upfile ;//print_r($upfile);
//页面加载的时候就调用
downloadFile("uploads\"."".$_GET['file'],");//$filePath是服务器的文件地址
//$saveAsFileName是用户指定的下载后的文件名
function downloadFile($filePath,$saveAsFileName){// 清空缓冲区并关闭输出缓冲ob_end_clean(); //r: 以只读方式打开,b: 强制使用二进制模式$fileHandle=fopen($filePath,"rb"); if($fileHandle===false){echo "没有发现文件: $filePathn";exit;}Header("Content-type: application/octet-stream");Header("Content-Transfer-Encoding: binary");Header("Accept-Ranges: bytes");Header("Content-Length: ".filesize($filePath));Header("Content-Disposition: attachment; filename="$saveAsFileName"");while(!feof($fileHandle)) {//从文件指针 handle 读取最多 length 个字节echo fread($fileHandle, 32768); }fclose($fileHandle);
}
?>
本文发布于:2024-02-04 05:37:52,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/170700258852679.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |