建设使用PHP RecursiveDirectoryIterator一个简单的目录浏览器 [英] building a simple directory browser using php RecursiveDirectoryIterator
本文介绍了建设使用PHP RecursiveDirectoryIterator一个简单的目录浏览器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
喜
我想建立简单的目录浏览器来浏览文件夹和子文件夹uing PHP RecursiveDirectoryIterator ..我需要的是如何创造这种帮助。我已经开始与以下code。
$ DIR =目录名(__ FILE__); //目录的路径阅读
$ =迭代器新RecursiveDirectoryIterator($目录);
的foreach(新RecursiveIteratorIterator($迭代器,RecursiveIteratorIterator :: CHILD_FIRST)为$文件){
如果(文件 - $>!ISFILE()){//创建超级链接,如果这是一个文件夹
回声&下; A HREF =。 $文件 - >的getPath()>中。 。 $文件 - >用GetFileName()。 \\&下; / A>中;
}其他{//不要链接,如果这是一个文件
$文件 - >用GetFileName()
}
}
解决方案
让我code,对于你....
< PHP
$根= __DIR__;功能is_in_dir($文件,$目录$递归= TRUE,$限制= 1000){
$目录的真实路径=($目录);
$父=真实路径($文件);
$ I = 0;
而($父){
如果($目录== $父)返回true;
如果($父目录名==($父)|| $递归!)打破;
$父=目录名($父);
}
返回false;
}$ PATH = NULL;
如果(使用isset($ _ GET ['文件'])){
$ PATH = $ _GET ['文件'];
如果(!is_in_dir($ _ GET ['文件'],$根)){
$ PATH = NULL;
}其他{
$ PATH ='/'.$path;
}
}如果(is_file($根。$ PATH)){
ReadFile的($ $根路径);
返回;
}如果($ PATH)回声'< A HREF =?文件='。urlen code(SUBSTR(目录名($根$ PATH)的strlen($根)+ 1))'取代。 < / A>< BR />';
的foreach(水珠($根源。$路径。'/ *')为$文件){
$文件的真实路径=($文件);
$链接= SUBSTR($文件,strlen的($根)+ 1);
呼应'< A HREF =文件=?'urlen code($链接)。'>'基本名($文件)。'< / A>< BR />';
}
Hi i am trying to build simple directory browser to browse folders and sub-folders uing php RecursiveDirectoryIterator .. i need help of how to create this. i have started with the following code.
$dir = dirname(__FILE__); //path of the directory to read
$iterator = new RecursiveDirectoryIterator($dir);
foreach (new RecursiveIteratorIterator($iterator, RecursiveIteratorIterator::CHILD_FIRST) as $file) {
if (!$file->isFile()) { //create hyperlink if this is a folder
echo "<a href=". $file->getPath().">" . $file->getFilename() . "\</a>";
}else{ //do not link if this is a file
$file->getFilename()
}
}
解决方案
Allow me to code that for you....
<?php
$root = __DIR__;
function is_in_dir($file, $directory, $recursive = true, $limit = 1000) {
$directory = realpath($directory);
$parent = realpath($file);
$i = 0;
while ($parent) {
if ($directory == $parent) return true;
if ($parent == dirname($parent) || !$recursive) break;
$parent = dirname($parent);
}
return false;
}
$path = null;
if (isset($_GET['file'])) {
$path = $_GET['file'];
if (!is_in_dir($_GET['file'], $root)) {
$path = null;
} else {
$path = '/'.$path;
}
}
if (is_file($root.$path)) {
readfile($root.$path);
return;
}
if ($path) echo '<a href="?file='.urlencode(substr(dirname($root.$path), strlen($root) + 1)).'">..</a><br />';
foreach (glob($root.$path.'/*') as $file) {
$file = realpath($file);
$link = substr($file, strlen($root) + 1);
echo '<a href="?file='.urlencode($link).'">'.basename($file).'</a><br />';
}
这篇关于建设使用PHP RecursiveDirectoryIterator一个简单的目录浏览器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文