我想要用php实现一个网页获取一个文件夹 E:\avi里所有后缀是avi和pdf文件的路径,然后把超链接放到网页。
文件夹avi可能是在二级或更高的路径,就是可能E:\avi\1.avi E:\1\x\1.avi 我需要
名字.avi路径用相对路径
$path = '..';
$ext = '.avi';
function get_filetree($path, $ext){
$tree = array();
foreach(glob($path.'/*'.$ext) as $single){
if(is_dir($single)){
$tree = array_merge($tree,get_filetree($single));
}
else{
$tree[] = $single;
}
}
return $tree;
}
print_r(get_filetree($path));
下面的代码,测试过了,可以返回指定类型的文件,如果需要,可以修改路径和后缀名。
<?php
$path = '.';
$ext = '.avi';
function get_filetree($path, $ext){
$tree = array();
$temp = glob($path.'/*'.$ext);
if(!!$temp) $tree += $temp;
foreach(glob($path.'/*', GLOB_ONLYDIR) as $single){
$temp = get_filetree($single, $ext);
if(!!$temp) $tree += $temp;
}
return $tree;
}
print_r(get_filetree($path, $ext));
<?php
$path = ".";
$ext = ".avi";
function get_filetree($path, $ext)
{
$tree = array();
$temp = glob($path . "/*" . $ext);
if($temp) $tree = array_merge($tree, $temp);
foreach (glob($path . "/*", GLOB_ONLYDIR) as $dir) {
$temp = get_filetree($dir, $ext);
if($temp) $tree = array_merge($tree, $temp);
}
return $tree;
}
print_r(get_filetree($path, $ext));
我上面最后一次回答的代码是否可以获取你目录下的所有avi文件了?我在网站服务器上测试获取css类型的文件是可以的。
上面的答案有帮助吗?如果还有问题,请提出来,如果对答案满意,请顶一下,并标记为采纳答案,谢谢!
根据多位大神结果
php获取同文件夹内:php视频的所有avi格式视频
<!doctype html>
<html>
<head>
<meta charset="GB2312">
<title>java</title>
<link href="/intranet/css/gong.css" rel="stylesheet" type="text/css">
</head>
<body>
<?php
$path = ".";
$ext = ".avi";
function get_filetree($path, $ext)
{
$tree = array();
$temp = glob($path . "/*" . $ext);
if($temp) $tree = array_merge($tree, $temp);
foreach (glob($path . "/*", GLOB_ONLYDIR) as $dir) {
$temp = get_filetree($dir, $ext);
if($temp) $tree = array_merge($tree, $temp);
}
return $tree;
}
$list = get_filetree($path, $ext);
foreach($list as $item) {
$fd=substr($item, 1);
echo "<a href=\"/php视频教程".$fd.'">';
echo "".$file = basename($item)."</a>";
echo "<br/>";
}
?>
</body>
输出文件名