• List all files and subfolders in PHP.
    12 replies, posted
Hey, say I have a following folder structure: clients/ clients/anly/ clients/anly/intro.txt clients/anly/files/index.php clients/anly/files/viewpage.php clients/anly/files/css/main.css What would you suggest using so that I can: Make it automatically read and list all files and subfolders in the format I specified, not just random file listing, I tried scandir but it gives it as: Array{ } And I don't know how to format the way array outputs so maybe someone can tell me how to do it? I tried writing: [code]$directories = scandir("clients/".$row['folder']); print_r($directories);[/code] yet output was: "Array ( [0] => . [1] => .. [2] => Introduction.txt [3] => files )" so yeah it didn't even scan files inside files folder. Can someone help? So briefly: How do I scan and output list of all files in subfolders of specified dir, and how do I format the way array shows up? Thanks.
[php] <?php function recursive_scandir($dir){ $contents = array(); foreach(scandir($dir) as $file){ $path = $dir.DIRECTORY_SEPARATOR.$file; if(is_dir($path)){ $contents = array_merge($contents, recursive_scandir($path)); } else { $contents[] = $path; } } return $contents; } [/php] That should work. First time writing PHP in ages so it might not work.
[QUOTE=Jelly;36970200][php] <?php function recursive_scandir($dir){ $contents = array(); foreach(scandir($dir) as $file){ $path = $dir.DIRECTORY_SEPARATOR.$file; if(is_dir($path)){ $contents = array_merge($contents, recursive_scandir($path)); } else { $contents[] = $path; } } return $contents; } [/php] That should work. First time writing PHP in ages so it might not work.[/QUOTE] [QUOTE]Fatal error: Maximum function nesting level of '100' reached, aborting! in D:\Program Files (x86)\EasyPHP\EasyPHP-5.3.9\www\index.php on line 24[/QUOTE] What I got after calling: [php]recursive_scandir(dirname(__FILE__));[/php]
[QUOTE=camacazie638;36970317]What I got after calling: [php]recursive_scandir(dirname(__FILE__));[/php][/QUOTE] scandir() returns "." and ".." as well as all the other directory entries so unless you filter them out you'll recurse forever
[php] <?php function recursive_scandir($dir){ $contents = array(); foreach(scandir($dir) as $file){ if($file == '.' || $file == '..') continue; $path = $dir.DIRECTORY_SEPARATOR.$file; if(is_dir($path)){ $contents = array_merge($contents, recursive_scandir($path)); } else { $contents[] = $path; } } return $contents; } print_r(recursive_scandir(dirname(__FILE__))); [/php] Should work. [editline]28th July 2012[/editline] [QUOTE=swift and shift;36970522]scandir() returns "." and ".." as well as all the other directory entries so unless you filter them out you'll recurse forever[/QUOTE] Gotta love PHP.
[QUOTE=Jelly;36970535]Gotta love PHP.[/QUOTE] it's a unix thing [code] >> Dir.entries "/Users" => [".", "..", ".localized", "charlie", "Shared"] [/code]
[QUOTE=Jelly;36970535]Gotta love PHP.[/QUOTE] Every language does that.
thanks for the help, that worked. [editline]28th July 2012[/editline] but how can I style the output? It gives like: Array ( [0] => clients\anly\Introduction.txt [1] => clients\anly\files\css\main.css [2] => clients\anly\files\index.php [3] => clients\anly\files\viewpage.php )
^^use CSS
It just returns an array which means it stores several values. [PHP] <?php $stuff = recursive_scandir(dirname(__FILE__)); echo $stuff[0]; // first item in the array echo $stuff[1]; // second and so on... foreach($stuff as $item){ echo $item; } ?> [/PHP]
Wrote this a while ago... [PHP] <html> <head> <title>JamieHankins.co.uk</title> <link rel="stylesheet" href="http://jamiehankins.co.uk/css.css"> <style type="text/css"> table.tables td { border-width: 4px; padding: 8px; border-style: solid; border-color: #999999; } .ddimgtooltip{ box-shadow: 3px 3px 5px #818181; /*shadow for CSS3 capable browsers.*/ -webkit-box-shadow: 3px 3px 5px #818181; -moz-box-shadow: 3px 3px 5px #818181; display:none; position:absolute; border:1px solid black; background:white; color: black; z-index:2000; padding: 4px; width: 100px; height 100px; } .imagesize{ width: 90px; height 90px; max-height: 90px; max-width: 90px; } </style> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script> <script type="text/javascript"> var ddimgtooltip={ tiparray:function(){ var tooltips=[] <?php $count = 0; if ($handle = opendir('.')) { while (false !== ($entry = readdir($handle))) { if ($entry != "." && $entry != "..") { $count++; echo" "; echo "tooltips[$count - 1]=['$entry']"; } } } ?> return tooltips }(), tooltipoffsets: [20, -30], tipprefix: 'imgtip', createtip:function($, tipid, tipinfo){ if ($('#'+tipid).length==0){ return $('<div id="' + tipid + '" class="ddimgtooltip" />').html( '<div style="text-align:center"><img class="imagesize" src="' + tipinfo[0] + '" /></div>' + ((tipinfo[1])? '<div style="text-align:left; margin-top:5px">'+tipinfo[1]+'</div>' : '') ) .css(tipinfo[2] || {}) .appendTo(document.body) } return null }, positiontooltip:function($, $tooltip, e){ var x=e.pageX+this.tooltipoffsets[0], y=e.pageY+this.tooltipoffsets[1] var tipw=$tooltip.outerWidth(), tiph=$tooltip.outerHeight(), x=(x+tipw>$(document).scrollLeft()+$(window).width())? x-tipw-(ddimgtooltip.tooltipoffsets[0]*2) : x y=(y+tiph>$(document).scrollTop()+$(window).height())? $(document).scrollTop()+$(window).height()-tiph-10 : y $tooltip.css({left:x, top:y}) }, showbox:function($, $tooltip, e){ $tooltip.show() this.positiontooltip($, $tooltip, e) }, hidebox:function($, $tooltip){ $tooltip.hide() }, init:function(targetselector){ jQuery(document).ready(function($){ var tiparray=ddimgtooltip.tiparray var $targets=$(targetselector) if ($targets.length==0) return var tipids=[] $targets.each(function(){ var $target=$(this) $target.attr('rel').match(/\[(\d+)\]/) var tipsuffix=parseInt(RegExp.$1) var tipid=this._tipid=ddimgtooltip.tipprefix+tipsuffix var $tooltip=ddimgtooltip.createtip($, tipid, tiparray[tipsuffix]) $target.mouseenter(function(e){ var $tooltip=$("#"+this._tipid) ddimgtooltip.showbox($, $tooltip, e) }) $target.mouseleave(function(e){ var $tooltip=$("#"+this._tipid) ddimgtooltip.hidebox($, $tooltip) }) $target.mousemove(function(e){ var $tooltip=$("#"+this._tipid) ddimgtooltip.positiontooltip($, $tooltip, e) }) if ($tooltip){ $tooltip.mouseenter(function(){ ddimgtooltip.hidebox($, $(this)) }) } }) }) } } ddimgtooltip.init("*[rel^=imgtip]") </script> </head> <body> <div class="box"> <span class="title">Screenshot Bin</span> <table class="tables"> <tr> <?php $COUNT = 0; if ($handle = opendir('.')) { while (false !== ($entry = readdir($handle))) { if ($COUNT % 3 == 0) { echo "</tr><tr><td><a href='$entry' rel='imgtip[$COUNT]'>$entry</a></td>"; $COUNT++; } elseif ($entry == "index.php" or $entry == ".." or $entry == ".") { echo "<!--Removing $entry from list due to blacklist-->"; } else { echo "<td><a href='$entry' rel='imgtip[$COUNT]'>$entry</a></td>"; $COUNT++; } } closedir($handle); } ?> </tr> </table> </div> <div class="proboxs"> <a class="social" href="http://jamiehankins.co.uk/">Back</a> </div> </body> </html> [/PHP]
[QUOTE=jamie1130;37267652]Wrote this a while ago... [noparse][CODE STUFF][/noparse][/QUOTE] What's with the line number styling in chrome? It breaks..
[QUOTE=h2ooooooo;37268380]What's with the line number styling in chrome? It breaks..[/QUOTE] Same. I'm wondering if it was a piss up on my end. [url]https://dl.dropbox.com/u/34137666/ShareX/2012-08-17_03-31-54.png[/url]
Sorry, you need to Log In to post a reply to this thread.