- //擷取指定目錄下的檔案清單
- //$path 指定的目錄,預設為目前的目錄
- //$ifchild 是否顯示子目錄檔案清單,預設不顯示
- //$curpath 顯示當前的路徑,預設為從目前的目錄開始;這個主要是為了顯示確定href路徑
- function openpath($path=".",$ifchild=false,$curpath=".")
- {
- $handle = opendir($path);
- if($handle)
- {
- while(false !== ($file = readdir($handle)))
- {
- if ($file != "." && $file != "..")
- {
- $fullPath = $path.DIRECTORY_SEPARATOR.$file;
- if(is_dir($fullPath))//如果是目錄檔案
- {
- if($ifchild)//如果設定了顯示子目錄
- {
- //遞迴
- openpath($path.DIRECTORY_SEPARATOR.$file,$ifchild,$curpath.DIRECTORY_SEPARATOR.$file);
- }
- else
- {
- echo "
- $file
\n";
- }
- }
- else if($file != basename(__FILE__))//排除當前執行指令碼
- {
- echo "
- $file
\n";
- }
- else
- {
- echo $file;
- }
- }
- }
- }
- closedir($handle);
- }
-
複製代碼因為要提供直接選取的功能,要是有一個下拉式功能表,裡面有待選路徑的顯示就好了。 2、取得當前檔案下所有子檔案路徑的代碼:
- /*擷取指定目錄檔案路徑列表
- *$path 指定的目錄,預設為目前的目錄
- *$ifchild 是否擷取子目錄檔案清單,預設不擷取
- *$curpath 顯示當前的路徑,預設為從目前的目錄開始
- *&$pach_html_srt 傳遞一個外部變數的引用進來,因為此方法有可能被遞迴調用,所以以這樣的方式來儲存
- * 一些資訊,也可以用全域變數來實現,在函數內部變數改變也影響到外部。
- *&$path_ref_count 原理同上,一個計數標誌,如果遞迴,計數器從上一次儲存的值開始自增
- */
- function openpath($path=".",$ifchild=false,&$path_html_str,&$path_ref_count)
- {
- $handle = opendir($path);
- if($handle)
- {
- while(false !== ($file = readdir($handle)))
- {
- if ($file != "." && $file != "..")
- {
- $fullPath = $path.DIRECTORY_SEPARATOR.$file;
- if(is_dir($fullPath))//如果檔案是目錄
- {
- $path_html_str.='
- ';
- $path_html_str.=$file.'
';
- if($ifchild)
- {
- //遞迴
- openpath($path.DIRECTORY_SEPARATOR.$file,$ifchild,&$path_html_str,&$path_ref_count);
- }
- $path_html_str.='
';
- }
- }
- }
- }
- closedir($handle);
- }
複製代碼有了上面的方法,就可以在前台用jquery mcDropdown外掛程式來讓使用者可以通過下拉式功能表選擇想進入的目錄,所以需要封裝成指定格式:
- $path_ref_count = 1;
- $path_html_str ='';
- openpath(".",true,&$path_html_str,&$path_ref_count);
- $path_html_str = '
';
- $path_html_str = str_replace ( "
", '', $path_html_str );
- ?>
複製代碼這樣把$path_html_str傳到前台,顯示出來就是一個符合mcDropdown要求的無序列表,就可以顯示相應的待選列表了。 完整代碼如下:1、test.html
-
-
-
-
- jquery mcDropdown實現檔案路徑可在下拉框選擇的方法_bbs.it-home.org
-
-
-
-
-
-
-
-
-
- Please select a category:
-
-
- #categorymenu#
-
-
複製代碼2、test.php
- //目錄資訊處理
- $path_ref_count = 1;
- $path_html_str ='';
- openpath(".",true,&$path_html_str,&$path_ref_count);
- $path_html_str = '
';
- $path_html_str = str_replace ( "
", '', $path_html_str );
- //var_dump($path_info);
- //var_dump($path_html_str);
-
- $str_buffer = file_get_contents (dirname(__FILE__).DIRECTORY_SEPARATOR.'test.html');
- $str_buffer = str_replace ( "#categorymenu#", $path_html_str, $str_buffer );
- $str_buffer = str_replace ( "#delim#", DIRECTORY_SEPARATOR, $str_buffer );
- echo $str_buffer;
-
-
- /*擷取指定目錄檔案路徑列表
- *$path 指定的目錄,預設為目前的目錄
- *$ifchild 是否擷取子目錄檔案清單,預設不擷取
- *$curpath 顯示當前的路徑,預設為從目前的目錄開始
- *&$pach_html_srt 傳遞一個外部變數的引用進來,因為此方法有可能被遞迴調用,所以以這樣的方式來儲存
- * 一些資訊,也可以用全域變數來實現,在函數內部變數改變也影響到外部。
- *&$path_ref_count 原理同上,一個計數標誌,如果遞迴,計數器從上一次儲存的值開始自增
- */
- function openpath($path=".",$ifchild=false,&$path_html_str,&$path_ref_count)
- {
- $handle = opendir($path);
- if($handle)
- {
- while(false !== ($file = readdir($handle)))
- {
- if ($file != "." && $file != "..")
- {
- $fullPath = $path.DIRECTORY_SEPARATOR.$file;
- if(is_dir($fullPath))//如果檔案是目錄
- {
- $path_html_str.='
- ';
- $path_html_str.=$file.'
';
- if($ifchild)
- {
- //遞迴
- openpath($path.DIRECTORY_SEPARATOR.$file,$ifchild,&$path_html_str,&$path_ref_count);
- }
- $path_html_str.='
';
- }
- }
- }
- }
- closedir($handle);
- }
- ?>
-
複製代碼jquery mcDropdown 外掛程式下載地址:http://www.givainc.com/labs/mcdropdown_jquery_plugin.htm。 |