一个困扰偶很久的目录遍历问题 这是以前做的一个类似dos下的deltree命令的函数,用的就是递归。可以参考一下。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 <?function deltree($dir){ chdir($dir); $handle=opendir('.'); while (($file=readdir($handle))<>"") { if(is_file($file)) unlink($file); if(is_dir($file) && $file<>"." && $file<>"..") deltree($file); } closedir($handle); }deltree('test');?> while ($filename = readdir($handle)) =>while (($filename = readdir($handle) !==false) Get($newpath,&$a);=>Get($newpath,$a);既然是在windows环境中,就将/变为\好了。 网上有很多这样的代码呵,我也不知是谁写的了<?$FileCount = 0; //文件个数$DirectoryCount = 0; //目录个数function FCount($Path){global $FileCount,$DirectoryCount; $Handle = opendir($Path); while($File = readdir($Handle)) { if(filetype($Path.$File) != 'dir') { echo "----文件名----$File<br>"; $FileCount++; } if($File !='.' && $File !='..' && filetype($Path.$File)=='dir') { echo "目录名----$File<br>"; $DirectoryCount++; FCount($Path."$File/"); } } closedir($Handle);}FCount('./hyforum_code/');echo "<br>请您注意脚本程序30秒钟超时警告<br><br>目录个数……………………$DirectoryCount<br>----文件个数……………………$FileCount<br>";?> 我写的一个删除程序, 没发现有什么遇目录名为0删不掉的问题.function delfile($path){ echo "<dl>"; echo "<dt>删除目录 <font color=green>".$path." </font>……<br>"; $handle=@opendir($path); @chdir($path); while (($file=@readdir($handle))!="") { if (($file==".") OR ($file=="..")) continue; if (is_dir($file)) delfile($file); else { echo "<dd>删除文件 <font color=green>".$file."</font> …… "; if (@unlink($file)) echo "<font color=blue>成功!</font><br>"; else echo "<font color=red>失败!</font><br>"; } } @closedir($handle); @chdir(".."); echo "<dt>删除目录 <font color=green>".$path."</font> …… "; if (@rmdir($path)) echo "<font color=blue>成功!</font><br>"; else echo "<font color=red>失败!</font><br>"; echo "</dl>";}?> 遇到目录名为0的会删不掉: 这是因为类似于while($File = readdir($Handle))的判断, 会把"0"转成0, 结果为假. uGain的和我的就不会.但是uGain的程序不能正确删除子目录, 因为if(is_dir($file) && $file<>"." && $file<>"..")这个判断有问题, PHP中好像没有<>...是!=才对吧. 不知道为什么PHP竟然不报错. 呵呵~ 楼上的,做个简单的测试吧。<?if(1<>2) echo "hahahahaha";?> 把while ($filename = readdir($handle))改为while (($filename = readdir($handle))!="") uGain, 我查了手册了, sorry, <>和!=是一样的, 我没注意:)不过你的程序的确是不行:P 呵呵,是啊,当时没有考虑目录名为“0”的情况。好象在linux下,0是无效目录名吧。 uGain, 你的程序不是"0"的问题, 一般的目录都删除不掉...... while ($filename = readdir($handle))=>while ($filename = readdir(strval($handle))) php在交叉编译到MIPS平台,无法获取系统时间 PHP新手请问 初学,有没有什么好一点的书籍推荐?谢谢!!^_^ 关于数据库or和and的用法 php连接access的问题 关于SNS网站中不同用户不同页面的实现 php怎么取得上面列表框中所有的值啊 PHP 怎么实现分页查询并把查询的数据以word文档下载下来 PHP中Serializable接口如何使用 一位网友送的空间和域名,开了一个论坛,速度飞快。特此感谢。也为里面的PHP版做个广告。 问个数组问题?? PHP中如何获得上传文件的扩展名?
function deltree($dir){
chdir($dir);
$handle=opendir('.');
while (($file=readdir($handle))<>"") {
if(is_file($file))
unlink($file);
if(is_dir($file) && $file<>"." && $file<>"..")
deltree($file);
}
closedir($handle);
}
deltree('test');
?>
=>while (($filename = readdir($handle) !==false)
Get($newpath,&$a);
=>Get($newpath,$a);
既然是在windows环境中,就将/变为\好了。
我也不知是谁写的了
<?
$FileCount = 0; //文件个数
$DirectoryCount = 0; //目录个数
function FCount($Path)
{
global $FileCount,$DirectoryCount;
$Handle = opendir($Path);
while($File = readdir($Handle))
{
if(filetype($Path.$File) != 'dir')
{
echo "----文件名----$File<br>";
$FileCount++;
}
if($File !='.' && $File !='..' && filetype($Path.$File)=='dir')
{
echo "目录名----$File<br>";
$DirectoryCount++;
FCount($Path."$File/");
}
}
closedir($Handle);
}FCount('./hyforum_code/');
echo "<br>请您注意脚本程序30秒钟超时警告<br><br>目录个数……………………$DirectoryCount<br>----文件个数……………………$FileCount<br>";
?>
{
echo "<dl>";
echo "<dt>删除目录 <font color=green>".$path." </font>……<br>"; $handle=@opendir($path);
@chdir($path); while (($file=@readdir($handle))!="")
{
if (($file==".") OR ($file=="..")) continue; if (is_dir($file))
delfile($file);
else
{
echo "<dd>删除文件 <font color=green>".$file."</font> …… ";
if (@unlink($file))
echo "<font color=blue>成功!</font><br>";
else
echo "<font color=red>失败!</font><br>";
}
} @closedir($handle); @chdir(".."); echo "<dt>删除目录 <font color=green>".$path."</font> …… ";
if (@rmdir($path))
echo "<font color=blue>成功!</font><br>";
else
echo "<font color=red>失败!</font><br>";
echo "</dl>";
}
?>
<?
if(1<>2) echo "hahahahaha";
?>
while ($filename = readdir($handle))
改为
while (($filename = readdir($handle))!="")
不过你的程序的确是不行:P
好象在linux下,0是无效目录名吧。
=>while ($filename = readdir(strval($handle)))