php mcDropdown實現檔案路徑可在下拉框選擇的方法

來源:互聯網
上載者:User
  1. //擷取指定目錄下的檔案清單
  2. //$path 指定的目錄,預設為目前的目錄
  3. //$ifchild 是否顯示子目錄檔案清單,預設不顯示
  4. //$curpath 顯示當前的路徑,預設為從目前的目錄開始;這個主要是為了顯示確定href路徑
  5. function openpath($path=".",$ifchild=false,$curpath=".")
  6. {
  7. $handle = opendir($path);
  8. if($handle)
  9. {
  10. while(false !== ($file = readdir($handle)))
  11. {
  12. if ($file != "." && $file != "..")
  13. {
  14. $fullPath = $path.DIRECTORY_SEPARATOR.$file;
  15. if(is_dir($fullPath))//如果是目錄檔案
  16. {
  17. if($ifchild)//如果設定了顯示子目錄
  18. {
  19. //遞迴
  20. openpath($path.DIRECTORY_SEPARATOR.$file,$ifchild,$curpath.DIRECTORY_SEPARATOR.$file);
  21. }
  22. else
  23. {
  24. echo "
  25. $file
  26. \n";
  27. }
  28. }
  29. else if($file != basename(__FILE__))//排除當前執行指令碼
  30. {
  31. echo "
  32. $file
  33. \n";
  34. }
  35. else
  36. {
  37. echo $file;
  38. }
  39. }
  40. }
  41. }
  42. closedir($handle);
  43. }
複製代碼

因為要提供直接選取的功能,要是有一個下拉式功能表,裡面有待選路徑的顯示就好了。

2、取得當前檔案下所有子檔案路徑的代碼:

  1. /*擷取指定目錄檔案路徑列表
  2. *$path 指定的目錄,預設為目前的目錄
  3. *$ifchild 是否擷取子目錄檔案清單,預設不擷取
  4. *$curpath 顯示當前的路徑,預設為從目前的目錄開始
  5. *&$pach_html_srt 傳遞一個外部變數的引用進來,因為此方法有可能被遞迴調用,所以以這樣的方式來儲存
  6. * 一些資訊,也可以用全域變數來實現,在函數內部變數改變也影響到外部。
  7. *&$path_ref_count 原理同上,一個計數標誌,如果遞迴,計數器從上一次儲存的值開始自增
  8. */
  9. function openpath($path=".",$ifchild=false,&$path_html_str,&$path_ref_count)
  10. {
  11. $handle = opendir($path);
  12. if($handle)
  13. {
  14. while(false !== ($file = readdir($handle)))
  15. {
  16. if ($file != "." && $file != "..")
  17. {
  18. $fullPath = $path.DIRECTORY_SEPARATOR.$file;
  19. if(is_dir($fullPath))//如果檔案是目錄
  20. {
  21. $path_html_str.='
  22. ';
  23. $path_html_str.=$file.'
      ';
    • if($ifchild)
    • {
    • //遞迴
    • openpath($path.DIRECTORY_SEPARATOR.$file,$ifchild,&$path_html_str,&$path_ref_count);
    • }
    • $path_html_str.='
  24. ';
  25. }
  26. }
  27. }
  28. }
  29. closedir($handle);
  30. }
複製代碼

有了上面的方法,就可以在前台用jquery mcDropdown外掛程式來讓使用者可以通過下拉式功能表選擇想進入的目錄,所以需要封裝成指定格式:

  1. $path_ref_count = 1;
  2. $path_html_str ='';
  3. openpath(".",true,&$path_html_str,&$path_ref_count);
  4. $path_html_str = '
      '.$path_html_str.'
    ';
  5. $path_html_str = str_replace ( "
      ", '', $path_html_str );
    • ?>
    複製代碼

    這樣把$path_html_str傳到前台,顯示出來就是一個符合mcDropdown要求的無序列表,就可以顯示相應的待選列表了。

    完整代碼如下:1、test.html

    1. jquery mcDropdown實現檔案路徑可在下拉框選擇的方法_bbs.it-home.org
    2. Please select a category:
    3. #categorymenu#
    複製代碼

    2、test.php

    1. //目錄資訊處理
    2. $path_ref_count = 1;
    3. $path_html_str ='';
    4. openpath(".",true,&$path_html_str,&$path_ref_count);
    5. $path_html_str = '
        '.$path_html_str.'
      ';
    6. $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。

    7. 聯繫我們

      該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

      如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

      A Free Trial That Lets You Build Big!

      Start building with 50+ products and up to 12 months usage for Elastic Compute Service

      • Sales Support

        1 on 1 presale consultation

      • After-Sales Support

        24/7 Technical Support 6 Free Tickets per Quarter Faster Response

      • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.