zipp Posted July 28, 2007 Share Posted July 28, 2007 I have been working on a script for a couple days now. I finally am about to give up on it. I cant seem to get anything about it working. What I am trying to do is get a printout of files and folders similar to this: upload/ ---file1 ---file2 ---file3 upload/folder1 ---file1 ---file2 ---file3 upload/folder2 ---file1 ---file2 ---file3 etc... I have been able to get some output, but it comes out all messed up. It looks like: ---file1 ---file2 ---file2 ---file3 ---file3 Any help would be much appreciated. Quote Link to comment Share on other sites More sharing options...
zipp Posted July 28, 2007 Author Share Posted July 28, 2007 Here is the code that I am working with: <?php $path = "upload/"; $dir_handle = @opendir($path) or die("Unable to open $path"); while ($file = readdir($dir_handle)) { if($file == "." || $file == ".." || $file == "index.php" ) continue; $name = $path .''. $file; if (!is_dir($name)){ for ($files = 0; $files < count($path); $files++){ echo "<strong>Files</strong>:<br>"; echo "<a href=\"$name\">$file</a><br>"; echo "<br>"; $files++; } } //print_r(glob($path . '*', GLOB_ONLYDIR)); if (is_dir($name)){ for ($folders = 0; $folders < count(glob("$path/*", GLOB_ONLYDIR)); $folders++){ echo "<a href=\"$name[$folders]\">$file</a><br>"; echo "<br>"; $folders++; } } } closedir($dir_handle); ?> Quote Link to comment Share on other sites More sharing options...
corbin Posted July 29, 2007 Share Posted July 29, 2007 I saw a recursive function once in the php documentation for listing directories/files.... I couldn't find it, but I need something similar for my own scripts soon, so I went ahead and coded something.... I didn't comment it, but it's fairly straight forward. <?php function ListFiles($folder) { $out = array(); $han = opendir($folder); $f = (strlen($folder) > 1) ? $folder . '/' : ''; while($it = readdir($han)) { if($it == '.' || $it == '..') continue; if(!is_dir($f.$it)) { $out[] = (strlen($folder) > 0) ? $folder . '/' . $it : $it; continue; } $path = (strlen($folder) > 0) ? $folder . '/' . $it : $it; $out = array_merge($out, ListFiles($path)); } return $out; } $out = listfiles('folder'); print_r($out); /* This is the output from tree folder /F /A in cmd -------------------- Y:\public_html\corbin\common>tree folder /A /F Folder PATH listing for volume Web_Files Volume serial number is E054-226C Y:\PUBLIC_HTML\CORBIN\COMMON\FOLDER | this is a file.txt | \---subfolder file in subfolder.txt -------------------- output from the print_r Array ( [0] => folder/New Folder/New Text Document.txt [1] => folder/New Text Document.txt ) ?> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.