我想要用php实现一个网页获取一个文件夹 E:\avi里所有后缀是avi和pdf文件的路径,然后把超链接放到网页。
文件夹avi可能是在二级或更高的路径,就是可能E:\avi\1.avi E:\1\x\1.avi 我需要
名字.avi路径用相对路径
希望大神可以给源码
根据多位大神,php获取服务器文件夹所有文件源码
<!doctype html>
<html>
<head>
<meta charset="GB2312">
<title>php</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>
这个获取客户端的不行的,
如果是你站内的可以这样:
function my_scandir($dir)
{
$files = array();
if ( $handle = opendir($dir) ) {
while ( ($file = readdir($handle)) !== false )
{
if ( $file != ".." && $file != "." )
{
if ( is_dir($dir . "/" . $file) )
{
$files[$file] = my_scandir($dir . "/" . $file);
}
else
{ if(fileSuffix($file) =='avi' or fileSuffix($file) =='pdf' ){
$files[] =$dir . "/" . $file;
}
}
}
}
closedir($handle);
return $files;
}
}
function fileSuffix($filename){
return strtolower(trim(substr(strrchr($filename, '.'), 1)));
}
上面这个就是遍历某文件夹以及子文件夹文件并且保存在一个数组里的
php 遍历文件夹下的所有文件及文件夹
/**
function my_scandir($dir){
$files = array();
if( $handle = opendir($dir) ) {
while( ($file = readdir($handle)) !== false ){
if( $file != ".." && $file != "." ){
if( is_dir($dir . "/" . $file) ){
$files[$file] = my_scandir($dir . "/" . $file);
}
else{
if(fileSuffix($file) =='avi' or fileSuffix($file) =='pdf' ){
$files[] =$dir . "/" . $file;
}
}
}
}
closedir($handle);
return $files;
}
}
function fileSuffix($filename){
return strtolower(trim(substr(strrchr($filename, '.'), 1)));
}