我想写一个遍历目录获取此目录中的文件(网页文件)的文件名的代码,怎么写?
我写的如下的,但是试运行了没有反应,既不报错也不显示。
大家帮我看一下。
<?php
//扫描文件夹生成sitemap Google sitemap
header('content-type:text/html;charset=utf-8');
$dir="/";
$url=null;function get_path($path){
if(is_dir($path)==true){
//是目录
if($open=opendir($path)){
while($list=readdir($open)){
$url.=get_path($list);
continue;
}
closedir($open);
}
}elseif(file_exists($path)){
//是文件,需要检测是否是静态网页
$f=explode(".",$path);
$i<=count($f);
if($f[$i]==".html" or $f[$i]==".htm"){
$url.='<url><loc>http://www.google.com/'.$page.'</loc><changefreq>daily</changefreq></url>';
}else{
$url.=null;
}
}else{
//否则返回null
$url.=null;
}
return $url;
}//开始
$p=null;
$p=get_path($dir);
$p.=null;
echo $p;?>
我写的如下的,但是试运行了没有反应,既不报错也不显示。
大家帮我看一下。
<?php
//扫描文件夹生成sitemap Google sitemap
header('content-type:text/html;charset=utf-8');
$dir="/";
$url=null;function get_path($path){
if(is_dir($path)==true){
//是目录
if($open=opendir($path)){
while($list=readdir($open)){
$url.=get_path($list);
continue;
}
closedir($open);
}
}elseif(file_exists($path)){
//是文件,需要检测是否是静态网页
$f=explode(".",$path);
$i<=count($f);
if($f[$i]==".html" or $f[$i]==".htm"){
$url.='<url><loc>http://www.google.com/'.$page.'</loc><changefreq>daily</changefreq></url>';
}else{
$url.=null;
}
}else{
//否则返回null
$url.=null;
}
return $url;
}//开始
$p=null;
$p=get_path($dir);
$p.=null;
echo $p;?>
$dir = '/';
应该是相对于站点的根目录吧
WINDOWS会返回空 当然什么都不输出$dir = 'd:/wwwroot/';这样试试如果是LINUX 大多数情况会是权限不足 但PHP.INI里设置不输出错误
header('content-type:text/html;charset=utf-8');
$dir= realpath("D:\KuGou");
$page = "";
function get_path($path){
global $page;
$url = null;
if(is_dir($path)){
if($open = opendir($path)){
while( false !== ($list = readdir($open)) ){
if($list !== '.' && $list !== '..'){
$url .= get_path($path."/".$list);
}
}
closedir($open);
}
}else if( file_exists($path)){
//是文件,需要检测是否是静态网页
$f=explode(".",basename($path));
$i=count($f)-1;
if($f[$i]=="html" or $f[$i]=="htm"){
$url='<url><loc>http://www.google.com/'.$page.'</loc><changefreq>daily</changefreq></url>';
}else{
$url=null;
}
}
return $url;
}//开始
$p=get_path($dir);
echo $p;
?>
楼主试下吧,你那样会造成无限遍历,不是没有结果,而是没有处理完
这样不行啊,还是没有结果。
我的全部源码如下:<?php
echo (is_dir("www")==true)?'不是目录!':'是目录!';
$dir= realpath("D:\AppServ\www");
$page = "";
function get_path($path){
global $path;
$url .= null;
if(is_dir($path)){
if($open = opendir($path)){
while( false !== ($list = readdir($open)) ){
if($list !== '.' && $list !== '..'){
$url .= get_path($path."/".$list);
echo $path.'/'.$list;
echo '<br />';
}
}
closedir($open);
}
}else if(file_exists($path)){
//是文件,需要检测是否是静态网页
$f=explode(".",basename($path));
$i=count($f)-1;
if($f[$i]=="html" or $f[$i]=="htm"){
$url.=' <url> <loc>http://www.google.com/'.$page.' </loc> <changefreq>daily </changefreq> </url>';
}else{
$url.=null;
}
}
return $url;
} //开始
$p=get_path($dir);
echo $p;
?>
还是没有反应的啊。
DirectoryIterator和RecursiveDirectoryIterator php5中的目录迭代类
$i=count($f) - 1;
if($f[$i]==".html" or $f[$i]==".htm"){
$i=count($f) - 1; if($f[$i]=="html" or $f[$i]=="htm"){ 扩展名不带 点
//遍历目录下面的所有文件 有了文件名就都好办了 pathinfo()你应该用得到
function walkdir($dir){
$dir=realpath($dir);
$stack=array($dir);
while(NULL!==($dir=array_shift($stack))&&false!==($handle=opendir($dir))){
while($file=readdir($handle)){
if($file=='.'||$file==".."){
continue 1;
}
$path=$dir.'/'.$file;
if(is_dir($path)){
array_push($stack,$path);
}else{
//echo $path."<br />";
$result[]=$path;
}
}
}
return $result;
}