显示文件夹并建立这些文件夹的链接 [英] Displaying folders and making links of those folders

查看:137
本文介绍了显示文件夹并建立这些文件夹的链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找在PHP中建立一个目录浏览器。

  $ dir = dirname(__ FILE__);我的代码已经开始了,但需要有人帮我完成或修改它。 //读取目录的路径

$ iterator = new RecursiveDirectoryIterator($ dir);
foreach(new RecursiveIteratorIterator($ iterator,RecursiveIteratorIterator :: CHILD_FIRST)as $ file){
if(!$ file-> isFile()){
echo< a href = 。 $文件 - >的getPath() > 中。 。 $ file-> getPath()。 \< / A> 中;



$解析方案

while你开发这个,重要的是要认识到你正在处理两个不同的目录路径。一个路径是基于URL的,另一个是基于文档的。路径/会把你带到你的站点的根目录,C:\ some path\\\\\\\\\\\\\\\\\\\\\\\\\\\\\'这两个引用相同的位置,使用不同的手段。

这就是说,你将不得不使用基于URL的路径浏览你的界面(除非你不不关心公开你的文档路径 - 安全风险),但代码需要把你点击的内容转换成基于文档的路径。下面是一些可能有帮助的PHP函数。

$ $ p $ __ FILE__ - 为您提供当前PHP文件的文档路径
__DIR__或dirname(__ FILE__) - 给出当前文件在
处的文件路径getcwd() - 获取当前工作目录

另外,不要让链接进入文件路径,而是根据发布的数据发布文件路径并重新加载页面。

 < form name ='myform'method ='post'> 
< a href ='folderA'onclick =document.myform.path.value = this.getAttribute('href'); document.myform.submit(); return false;> Folder A<一个>
< a href ='folderB'onclick =document.myform.path.value = this.getAttribute('href'); document.myform.submit(); return false;>文件夹B<一个>
< input type ='hidden'name ='path'value =''/>
< / form>


I am looking to build a directory browser in PHP. I have just started my code, but need someone to help me complete or modify it.

$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>";
    }
}

解决方案

While you develop this, its important to recognize that you are dealing with two different directory paths. One path is URL based, the other is Document based. The path / would take you to the root of your site where C:\some path\www\ would also take you there. Both of these reference the same location, using different means.

With that said, you're going to have to use URL based paths the navigate your interface (unless you don't care about exposing your document path - SECURITY RISK), but the code needs to take what you've clicked and convert it to Document based paths. Here are some PHP functions which might help.

__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

Also, instead of having the link go to the file path, have it post the file path and reload the page based on the posted data.

<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>

这篇关于显示文件夹并建立这些文件夹的链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆