Home  >  Article  >  Backend Development  >  How to achieve unlimited directory traversal in PHP development

How to achieve unlimited directory traversal in PHP development

高洛峰
高洛峰Original
2016-11-29 09:38:481244browse

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


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn