Heim  >  Artikel  >  类库下载  >  php开发中如何实现无限遍历目录(代码实例 )

php开发中如何实现无限遍历目录(代码实例 )

高洛峰
高洛峰Original
2016-10-14 10:56:431219Durchsuche

在php开发中,我们会经常遇到各种各样的难题,想要实现php无限遍历目录,可就很难实现这一方法,经过在网上查询一些资料,阅读一些论坛,才发现php无限遍历目录并没有想象那么难,现在就看一下吧。

使用的函数有:

isset()判断某个变量是否定义

chdir() 将当前目录改变为指定的目录。

opendir() 打开目录。

readdir()读取目录。

getcwd()。获取当前目录。

还用到了for if GET传值 大概就这些东东:

下面是代码:

<?php
if(isset($_GET[&#39;id&#39;]))//判断是否传值
{
$s=str_replace(&#39; &#39;,&#39;+&#39;,$_GET[&#39;id&#39;]);
$s=base64_decode($s);//接受传递的值 值一般是要打开的目录的绝对路劲
chdir($s);//切换到要打开的目录
}
$a=opendir(&#39;.&#39;);//打开当前目录
while(false!==($c=readdir($a)))//循环遍历目录内容的文件名
{
if(is_dir($c))//判断是目录还是文件
{
if($c==&#39;.&#39;)
{
}
else
{
if($c==&#39;..&#39;)
{
$w=base64_encode(substr(getcwd(),0,strrpos(getcwd(),&#39;\\&#39;)));//当前路径加上目录名 就是目录的绝对路径
echo "上一级"."<a href="http://www.php1.cn/">
}else
{
$w=base64_encode(getcwd().&#39;\\&#39;.$c);//当前路径加上目录名 就是目录的绝对路径
echo "$c"."<a href="http://www.php1.cn/">
}
}
}
else
{
echo "$c 不是目录<br />";
}
}
?>

以上就是php无限遍历目录的全部内容,大家在学习的时候需要注意一个问题,就是路径一定要是绝对路径,千万不要弄错了,希望能帮助到大家。


Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn

In Verbindung stehende Artikel

Mehr sehen