这个是xx.php文件的代码开始
<?php
//文件下载的封装(超链接的方式)
function down_file($filename,$allowDownExt=array('zip','html','rar')){
if(!is_file($filename)||!is_readable($filename)){
return false;
}
$ext=strtolower(pathinfo($filename,PATHINFO_EXTENSION));
if(!in_array($ext, $allowDownExt)){
return false;
}
//发送请求头,告诉浏览器输出的是字节流
header('content-type:application/octet-stream');
//告诉浏览器文件是按照字节来计算的
header('Accept-Ranges:bytes');
//告诉浏览器文件的大小
header('Accept-Length:'.filesize($filename));
//告诉浏览器文件是按附件处理,并且告诉浏览器下载的文件的名称
header('Content-Disposition:attachment;filename='.basename($filename));
//读取文件的内容
readfile($filename);
exit;
}
?>这个是xx.php文件的代码结束
这个是index.html文件的代码开始
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Document</title> </head> <body> <a href="http://localhost:8081/download.php?filename=5.html">下载5.html</a> </body> </html>
这个是index.html文件的代码结束
这个是download.php文件的代码开始
<?php
require_once('xx.php');
$filename=$_GET['filename'];
down_file($filename);
?>这个是download.php文件的代码结束
立即学习“PHP免费学习笔记(深入)”;
相关推荐:
以上就是php的下载功能的详细内容,更多请关注php中文网其它相关文章!
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号