Recently used PHP to read the file directory information, on the Internet to get the following section of code:
Gets the list of files in the specified directory
$path The specified directory, tacitly considers the current directory
$ifchild whether to display a subdirectory file list, default does not display
$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
Copy Code code as follows:
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 the directory file
{
if ($ifchild)//If a display subdirectory is set
{
Recursion
Openpath ($path. Directory_separator. $file, $ifchild, $curpath. Directory_separator. $file);
}
Else
{
echo "<li><a href=\" $curpath/$file \ "target=\" _blank\ "> $file </a></li>\n";
}
}
else if ($file!= basename (__file__))//Exclude Current execution script
{
echo "<li><a href=\" $curpath/$file \ "target=\" _blank\ "> $file </a></li>\n";
}
Else
{
Echo $file;
}
}
}
}
Closedir ($handle);
}
Because you want to provide a path to select the function, it is best to have a drop-down menu, which has to be selected in the path of the display, so convenient a lot, so I changed the path to output into an unordered list.
Below is a code that gets all the file paths under the current files:
Copy Code code as follows:
/* Get list of specified directory file paths
* $path The specified directory, which defaults to the current directory
* $ifchild whether to get the subdirectory file list, default does not get
* $curpath Display the current path, default to start from current directory
*& $pach _HTML_SRT Pass a reference to an external variable, because this method might be called recursively, so save it in such a way
* Some information can also be implemented with global variables, and changes in the function internal variables also affect the external.
*& $path _ref_count principle Ibid., a count flag, if recursive, the counter starts to increase from the last saved value
*/
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))//If the file is a directory
{
$path _html_str.= ' <li rel= '. $path _ref_count++. ' " > ';
$path _html_str.= $file. ' <ul> ';
if ($ifchild)
{
Recursion
Openpath ($path. Directory_separator. $file, $ifchild,& $path _html_str,& $path _ref_count);
}
$path _html_str.= ' </ul></li> ';
}
}
}
}
Closedir ($handle);
}
With the above approach, I can use the jquery Mcdropdown plugin in the foreground to allow users to select the directory they want to go through the Pull-down menu, so they need to be encapsulated in the specified format:
Copy Code code as follows:
$path _ref_count = 1;
$path _html_str = ';
Openpath (".",true,& $path _html_str,& $path _ref_count);
$path _html_str = ' <ul id= ' categorymenu ' class= ' mcdropdown_menu ' > '. $path _html_str. ' </ul> ';
$path _html_str = Str_replace ("<ul></ul>", "", $path _html_str);
So I upload the $path_html_str to the foreground, the display is a meeting mcdropdown requirements of the unordered list, you can display the appropriate list of choices.
The complete code is as follows:
test.html
Copy Code code as follows:
<! DOCTYPE html>
<meta charset= "Utf-8"/>
<title>test</title>
<script type= "Text/javascript" src= "/lib/jquery.js" ></script>
<script type= "Text/javascript" src= "/lib/jquery.mcdropdown.js" ></script>
<script type= "Text/javascript" src= "/lib/jquery.bgiframe.js" ></script>
<!---//load the Mcdropdown CSS stylesheet//--->
<link type= "Text/css" href= "./css/jquery.mcdropdown.css" rel= "stylesheet" media= "All"/>
<script type= "Text/javascript" >
$ (document). Ready (function () {
var Delim = "#delim #";
Dd= $ ("#category"). Mcdropdown ("#categorymenu", {
Allowparentselect:true,
Delim:delim
});
});
</script>
<body>
<p>
Please select a category:<br/>
<input type= "text" name= "category" id= "category" Value= ""/>
</p>
#categorymenu #
</body>
test.php
Copy Code code as follows:
<?php
Directory Information Processing
$path _ref_count = 1;
$path _html_str = ';
Openpath (".",true,& $path _html_str,& $path _ref_count);
$path _html_str = ' <ul id= ' categorymenu ' class= ' mcdropdown_menu ' > '. $path _html_str. ' </ul> ';
$path _html_str = Str_replace ("<ul></ul>", "", $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;
/* Get list of specified directory file paths
* $path The specified directory, which defaults to the current directory
* $ifchild whether to get the subdirectory file list, default does not get
* $curpath Display the current path, default to start from current directory
*& $pach _HTML_SRT Pass a reference to an external variable, because this method might be called recursively, so save it in such a way
* Some information can also be implemented with global variables, and changes in the function internal variables also affect the external.
*& $path _ref_count principle Ibid., a count flag, if recursive, the counter starts to increase from the last saved value
*/
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))//If the file is a directory
{
$path _html_str.= ' <li rel= '. $path _ref_count++. ' " > ';
$path _html_str.= $file. ' <ul> ';
if ($ifchild)
{
Recursion
Openpath ($path. Directory_separator. $file, $ifchild,& $path _html_str,& $path _ref_count);
}
$path _html_str.= ' </ul></li> ';
}
}
}
}
Closedir ($handle);
}
?>
The jquery Mcdropdown plugin can be downloaded here: http://www.givainc.com/labs/mcdropdown_jquery_plugin.htm