Note that there are some explanatory texts on larger screens.

plurals
  1. POPHP Directory and Files Recursive Listing
    primarykey
    data
    text
    <p>I tried to edit this PHP code to specify a directory for recursive listing of files and sub directories but no joy.</p> <p>any ideas is greatly appreciated.</p> <p>I just found this code from this link:</p> <p><a href="https://stackoverflow.com/questions/3826963/php-list-all-files-in-directory/10228005#10228005">PHP list all files in directory</a></p> <p>And here's the code below:</p> <pre><code> &lt;?php /** * Recovers folder structure and files of a certain path * * @param string $path Folder where files are located * @param string $pattern Filter by extension * @param string $flags Flags to be passed to the glob * @return array Folder structure */ function getFolderTree($path) { //Recovers files and directories //$paths = glob($path . "*", GLOB_MARK | GLOB_ONLYDIR | GLOB_NOSORT); //@param string ($path ".\temp" ); //$path = "."; $paths = glob($path . "*", GLOB_MARK | GLOB_ONLYDIR | GLOB_NOSORT); $files = glob($path . "*"); //Traverses the directories found foreach ($paths as $key =&gt; $path) { //Create directory if exists //$directory = explode("//", $path); $directory = explode("\\", $path); unset($directory[count($directory) - 1]); $directories[end($directory)] = getFolderTree($path); //Verify if exists files foreach ($files as $file) { if (strpos(substr($file, 2), ".") !== false) $directories[] = substr($file, (strrpos($file, "\\") + 1)); } } //Return the directories if (isset($directories)) { return $directories; } //Returns the last level of folder else { $files2return = Array(); foreach ($files as $key =&gt; $file) $files2return[] = substr($file, (strrpos($file, "\\") + 1)); return $files2return; } } /** * Creates the HTML for the tree * * @param array $directory Array containing the folder structure * @return string HTML */ function createTree($directory) { $html = "&lt;ul&gt;"; foreach($directory as $keyDirectory =&gt; $eachDirectory) { if(is_array($eachDirectory)) { $html .= "&lt;li class='closed'&gt;&lt;span class='folder'&gt;" . $keyDirectory . "&lt;/span&gt;"; $html .= createTree($eachDirectory); $html .= "&lt;/li&gt;"; } else { $html .= "&lt;li&gt;&lt;span class='file'&gt;" . $eachDirectory . "&lt;/span&gt;&lt;/li&gt;"; } } $html .= "&lt;/ul&gt;"; return $html; } //Create output $pathx = 'temp/'; //$directory = getFolderTree($pathx); $directory = getFolderTree($pathx ); $htmlTree = createTree($directory[$pathx]); ?&gt; &lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt; &lt;html xmlns="http://www.w3.org/1999/xhtml"&gt; &lt;head&gt; &lt;meta http-equiv="content-type" content="text/html; charset=iso-8859-1"/&gt; &lt;title&gt;PHP Directories&lt;/title&gt; &lt;link rel="stylesheet" href="http://jquery.bassistance.de/treeview/jquery.treeview.css" /&gt; &lt;link rel="stylesheet" href="http://jquery.bassistance.de/treeview/demo/screen.css" /&gt; &lt;script src="http://jquery.bassistance.de/treeview/lib/jquery.js" type="text/javascript"&gt;&lt;/script&gt; &lt;script src="http://jquery.bassistance.de/treeview/lib/jquery.cookie.js" type="text/javascript"&gt;&lt;/script&gt; &lt;script src="http://jquery.bassistance.de/treeview/jquery.treeview.js" type="text/javascript"&gt;&lt;/script&gt; &lt;script type="text/javascript" src="http://jquery.bassistance.de/treeview/demo/demo.js"&gt;&lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;div id="main"&gt; &lt;ul id="browser" class="filetree"&gt; &lt;?php echo $htmlTree;?&gt; &lt;/ul&gt; &lt;/div&gt; &lt;/body&gt; &lt;/html&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload