1、php中的递归查找文件夹,php递归获取目录内的文件,包含子目录(转载自 https://blog.csdn.net/weixin_39734184/article/details/115537343?spm=1001.2101.3001.6650.1&utm_medium=distribute.pc_relevant.none-task-blog-2defaultCTRLISTdefault-1-115537343-blog-54885837.pc_relevant_default&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2defaultCTRLISTdefault-1-115537343-blog-54885837.pc_relevant_default&utm_relevant_index=2)
download.php
<?php
require_once 'functions.php';
//排空错误
if(empty($_GET['id'])){
die('id is empty');
}
//连接数据库
connnetDb();
$id=intval($_GET['id']);
function readFileFromDir($dir) {
if (!is_dir($dir)) {
return false;
}
//打开目录
$handle = opendir($dir);
while (($file = readdir($handle)) !== false) {
//排除掉当前目录和上一个目录
if ($file == "." || $file == "..") {
continue;
}
$file = $dir . DIRECTORY_SEPARATOR . $file;
//如果是文件就打印出来,否则递归调用
if (is_file($file)) {
print $file . '
';
} elseif (is_dir($file)) {
readFileFromDir($file);
}
}
}
$dir = '/phpstudy_pro/WWW/today/results';
readFileFromDir($dir);
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>显示文件</title>
</head>
<body>
<button>
<a href = "http://today:8090/results/.txt">
下载文件</button>
效果:获取文件目录并且显示在页面上
2、php三种方式下载文件
(转载自:https://www.php.cn/php-weizijiaocheng-390215.html)
1、直接添加文件链接
<button>
<a href = "http://localhost/down.zip">
下载文件</button>
2、传递参数查找并跳转到下载链接
<button>
<a href = "http://localhost?f='down'">
下载文件
</button>
<?php$down = $_GET['f']; //获取文件参数
$filename = $down.'.zip'; //获取文件名称
$dir ="down/"; //相对于网站根目录的下载目录路径$down_host = $_SERVER['HTTP_HOST'].'/'; //当前域名//判断如果文件存在,则跳转到下载路径if(file_exists(__DIR__.'/'.$dir.$filename)){
header('location:http://'.$down_host.$dir.$filename);
}else{
header('HTTP/1.1 404 Not Found');
}
3、head() 和 fread()函数把文件直接输出到浏览器
<?php
$file_name = "down";$file_name = "down.zip"; //下载文件名 $file_dir = "./down/"; //下载文件存放目录 //检查文件是否存在 if (! file_exists ( $file_dir . $file_name )) {
header('HTTP/1.1 404 NOT FOUND');
} else {
//以只读和二进制模式打开文件
$file = fopen ( $file_dir . $file_name, "rb" );
//告诉浏览器这是一个文件流格式的文件
Header ( "Content-type: application/octet-stream" );
//请求范围的度量单位
Header ( "Accept-Ranges: bytes" );
//Content-Length是指定包含于请求或响应中数据的字节长度
Header ( "Accept-Length: " . filesize ( $file_dir . $file_name ) );
//用来告诉浏览器,文件是可以当做附件被下载,下载后的文件名称为$file_name该变量的值。
Header ( "Content-Disposition: attachment; filename=" . $file_name );
//读取文件内容并直接输出到浏览器
echo fread ( $file, filesize ( $file_dir . $file_name ) );
fclose ( $file );
exit ();
}
效果:<button>
<a href = "http://today:8090?f='results'">
下载文件</button>
回跳转到html后台页面。file:///G:/phpstudy_pro/WWW/today/index.html
更换文件夹名字为group,结果一样。
4、PHP文件下载函数(代码)
https://www.php.cn/php-weizijiaocheng-233298.html#:~:text=php%E5%AE%9E%E7%8E%B0%E6%96%87%E4%BB%B6%E4%B8%8B%E8%BD%BD%E7%9A%84%E5%87%BD%E6%95%B0%EF%BC%8C%E5%85%B7%E4%BD%93%E5%87%BD%E6%95%B0%E4%BB%A3%E7%A0%81%E5%A6%82%E4%B8%8B%EF%BC%9A%20function%20download%20%28%24file_url%2C%24new_name%3D%27%27%29%20%7B%20if,%28%21isset%20%28%24file_url%29%7C%7Ctrim%20%28%24file_url%29%3D%3D%27%27%29%20%7B%20return%20%27500%27%3B
```php
function download($file_url,$new_name=''){
if(!isset($file_url)||trim($file_url)==''){
return '500';
}
if(!file_exists($file_url)){ //检查文件是否存在
return '404';
}
$file_name=basename($file_url);
$file_type=explode('.',$file_url);
$file_type=$file_type[count($file_type)-1];
$file_name=trim($new_name=='')?$file_name:urlencode($new_name).'.'.$file_type;
$file_type=fopen($file_url,'r'); //打开文件
//输入文件标签
header("Content-type: application/octet-stream");
header("Accept-Ranges: bytes");
header("Accept-Length: ".filesize($file_url));
header("Content-Disposition: attachment; filename=".$file_name);
//输出文件内容
echo fread($file_type,filesize($file_url));
fclose($file_type);
}