PHP Mcdropdown implementation file path can be selected in the dropdown box method

Source: Internet
Author: User
  1. Gets the list of files in the specified directory
  2. $path The specified directory, default to the current directory
  3. $ifchild whether to display a list of subdirectory files, which are not displayed by default
  4. $curpath Displays the current path, which is the default starting from the current directory; This is primarily to show the determination of the HREF path
  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))//If it is a directory file
  16. {
  17. if ($ifchild)//If the display subdirectory is set
  18. {
  19. Recursive
  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__))//Exclude Current execution script
  30. {
  31. echo "
  32. $file
  33. \ n ";
  34. }
  35. Else
  36. {
  37. Echo $file;
  38. }
  39. }
  40. }
  41. }
  42. Closedir ($handle);
  43. }
Copy Code

Because you want to provide the function of the path selection, if there is a drop-down menu, it is OK to select the path to display.

2. Obtain the code for all sub-file paths under the current file:

  1. /* Get a list of specified directory file paths
  2. * $path The specified directory, default to the current directory
  3. * $ifchild whether to get a list of subdirectory files, which is not acquired by default
  4. * $curpath Displays the current path, which defaults to starting from the current directory
  5. *& $pach _html_srt to pass in a reference to an external variable, because this method might be called recursively, so save it in this way
  6. * Some information can also be implemented with global variables, and changes in the function internal variables also affect the external.
  7. *& $path _ref_count principle, a count flag, if recursive, the counter from the last saved value from the beginning of the increment
  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))//If the file is a directory
  20. {
  21. $path _html_str.= '
  22. ';
  23. $path _html_str.= $file. '
      ';
    • if ($ifchild)
    • {
    • Recursive
    • Openpath ($path. Directory_separator. $file, $ifchild,& $path _html_str,& $path _ref_count);
    • }
    • $path _html_str.= '
  24. ';
  25. }
  26. }
  27. }
  28. }
  29. Closedir ($handle);
  30. }
Copy Code

With the above method, you can use the jquery Mcdropdown plugin in the foreground to allow the user to select the desired directory from the drop-down menu, so it needs to be encapsulated in the specified format:

    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);
    6. ?>
Copy Code

This $path_html_str to the foreground, which shows a list of unordered lists that meet the requirements of mcdropdown, and can display the corresponding list of choices.

The complete code is as follows: 1, test.html

    1. jquery Mcdropdown implements a file path that can be selected in the dropdown box _bbs.it-home.org
    2. Please select a Category:
    3. #categorymenu #
Copy Code

2, test.php

  1. Directory Information Processing
  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);
  7. Var_dump ($path _info);
  8. Var_dump ($path _html_str);
  9. $str _buffer = file_get_contents (dirname (__file__). Directory_separator. ' test.html ');
  10. $str _buffer = Str_replace ("#categorymenu #", $path _html_str, $str _buffer);
  11. $str _buffer = Str_replace ("#delim #", Directory_separator, $str _buffer);
  12. echo $str _buffer;
  13. /* Get a list of specified directory file paths
  14. * $path The specified directory, default to the current directory
  15. * $ifchild whether to get a list of subdirectory files, which is not acquired by default
  16. * $curpath Displays the current path, which defaults to starting from the current directory
  17. *& $pach _html_srt to pass in a reference to an external variable, because this method might be called recursively, so save it in this way
  18. * Some information can also be implemented with global variables, and changes in the function internal variables also affect the external.
  19. *& $path _ref_count principle, a count flag, if recursive, the counter from the last saved value from the beginning of the increment
  20. */
  21. function Openpath ($path = ".", $ifchild =false,& $path _html_str,& $path _ref_count)
  22. {
  23. $handle = Opendir ($path);
  24. if ($handle)
  25. {
  26. while (false!== ($file = Readdir ($handle)))
  27. {
  28. if ($file! = "." && $file! = "...")
  29. {
  30. $fullPath = $path. Directory_separator. $file;
  31. if (Is_dir ($fullPath))//If the file is a directory
  32. {
  33. $path _html_str.= '
  34. ';
  35. $path _html_str.= $file. '
      ';
    • if ($ifchild)
    • {
    • Recursive
    • Openpath ($path. Directory_separator. $file, $ifchild,& $path _html_str,& $path _ref_count);
    • }
    • $path _html_str.= '
  36. ';
  37. }
  38. }
  39. }
  40. }
  41. Closedir ($handle);
  42. }
  43. ?>
Copy Code

jquery mcdropdown plugin Download address: http://www.givainc.com/labs/mcdropdown_jquery_plugin.htm.

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    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.