2013-06-23 37 views
0

我想知道是否可以使用我的服務器的文件結構自動爲圖庫創建導航菜單。 目前我有一個簡單的「展示」與硬編碼鏈接到不同的圖像文件夾(使用jQuery和AJAX和PHP,我不太明白但學會了如何使用教程等)。基本上,我有三個文件:PHP構建圖庫導航

  • main.php
  • 的main.css
  • images.php

和我使用main.php硬鏈接調用images.php腳本將包含圖像的特定文件夾加載到主頁面上的div中。

這是我目前的導航設置:

<ul> 
    <li><a href="#" onclick="loadPage('images.php?dirname=images/animals')">Animals</a></li> 
    <li><a href="#" onclick="loadPage('images.php?dirname=images/people')">People</a></li> 
    <li><a href="#" onclick="loadPage('images.php?dirname=images/objects')">Objects</a></li> 
</ul> 

我的問題是:由於我的所有圖像都在子目錄下的「圖像」目錄,是有辦法,我可以只建立導航點(PHP腳本?)使用「圖像」中的子目錄的名稱? (例如,它是不斷更新,當我添加多個文件夾)

此外,由於某種原因,我不能讓我的腳本變量包括「images.php?目錄名稱=圖像/」,是有沒有辦法解決這個問題?

回答

1

如果他們都在images目錄,你可以指定一個$image_path

<?php 
$image_path = '/full/path/to/images'; 
if ($_GET['gallery']) { 
    $gallery_path = $image_path . '/' . $_GET['gallery']; 

    # if $_GET['gallery'] is `animals`: 
    # 
    # $gallery_path = '/full/path/to/images/animals' 

    # load your images within this path 
} 
?> 

而獲得的所有子目錄使用dir

<?php 
$image_path = '/full/path/to/images'; 
$d = dir($image_path); 
while (false !== ($entry = $d->read())) { 
    if (is_dir($image_path . $entry)) { 
     if (($entry != '.') || ($entry != '..')) { 
      echo $entry; # or print your html code for each directory. 
     } 
    } 
} 
?> 
+0

的'dir'例如沒有得到放進入答案,現在就在那裏。這將獲得指定目錄內所有子目錄的列表。 – bnlucas

+0

我現在如何忽略它。和..文件夾? –

+0

更新爲if(($ entry!='。')||($ entry!='..')){...} – bnlucas