显示文件夹并建立这些文件夹的链接
问题描述:
我期待在PHP中构建一个目录浏览器。我刚刚开始了我的代码,但需要有人帮助我完成或修改它。显示文件夹并建立这些文件夹的链接
$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()) {
echo "<a href=". $file->getPath().">" . $file->getPath() . "\</a>";
}
}
答
当你开发这个,它很重要的是要认识到你正在处理两个不同的目录路径。一个路径是基于URL的,另一个是基于文档的。路径/将带您到您的网站的根目录,其中C:\ some path \ www \也会将您带到那里。这两种方法都使用不同的方法参照相同的位置。这就是说,你将不得不使用基于URL的路径来浏览你的界面(除非你不关心公开你的文档路径 - 安全风险),但代码需要采取你所拥有的点击并将其转换为基于文档的路径。以下是一些可能有所帮助的PHP函数。
__FILE__ - gives you the document path to the current PHP file
__DIR__ or dirname(__FILE__) - gives you the document path to the folder the current file is at
getcwd() - gets the current working directory
而且,而不是链接到的文件路径,已经将其发布的文件路径,并重新加载基于已发布数据的页面。
<form name='myform' method='post'>
<a href='folderA' onclick="document.myform.path.value=this.getAttribute('href'); document.myform.submit(); return false;">Folder A</a>
<a href='folderB' onclick="document.myform.path.value=this.getAttribute('href'); document.myform.submit(); return false;">Folder B</a>
<input type='hidden' name='path' value='' />
</form>
“詹姆斯”其实我正在一个大学的最终项目......所以不能使用其他图书馆...我必须编写我自己的函数来完成此任务。 – mubashir 2011-06-02 18:42:12