2011-06-02 76 views
0

我期待在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>"; 
    } 
} 

回答

0

當你開發這個,它很重要的是要認識到你正在處理兩個不同的目錄路徑。一個路徑是基於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> 
+0

「詹姆斯」其實我正在一個大學的最終項目......所以不能使用其他圖書館...我必須編寫我自己的函數來完成此任務。 – mubashir 2011-06-02 18:42:12