note 61021 added to function.scandir

From: Date: Mon, 23 Jan 2006 22:14:57 +0000
Subject: note 61021 added to function.scandir
Groups: php.notes 
Request: Send a blank email to php-notes+get-102314@lists.php.net to get a copy of this message
in addition to "brodseba at brodseba dot com" from march 2005: Perfected the script to sort files and directories in natural alphabetic order and with files listed first. Also added a more relative directory path approach. And changed the javascripting for links in simple ordinary href's. Note that the script uses scandir from the pear package (since I'm obliged to use an older PHP version). <? // scandir function is for php 5.0 require_once '\pear\PHP\Compat\Function\scandir.php'; // sorting function // based on counting of dots function cmp($a, $b) { if ( substr_count($a,".") == substr_count($b,".") ) { if ( is_string($a) && is_string($b) ) { return strcasecmp($a, $b) > 0 ? 1 : -1; }elseif ( is_int($a) && is_int($b) ) { return $a > $b ? 1 : -1; }elseif ( is_int($a) && is_string($b) ) { return 1; }elseif ( is_string($a) && is_int($b) ) { return -1; }else { return 0; }} return substr_count($a,".") < substr_count($b,".") ? 1 : -1; } function ls_recursive2($dir) { if (is_dir($dir)) { $files = scandir($dir); // needed to sort array usort ($files, "cmp"); foreach ($files as $file) { $currentfile = $dir . "/" . $file; $last_dir = ""; // calculate the identation. $count = substr_count($currentfile, '/'); $minus_count = substr_count($_SERVER['SCRIPT_FILENAME'], '/'); $count -= ($minus_count +1); for($p = 0; $p < $count; $p++) { $last_dir .= "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"; } // output with calculated identation // first in case of directories // run function again for directory if (is_dir($currentfile)) { if ($file != '.' && $file != '..') { $sub_len = strlen(dirname($_SERVER['SCRIPT_FILENAME']))+1; $java_targ = substr($currentfile, $sub_len); $last_dir .= "&nbsp;&nbsp;<img src='folder.png' alt='' align='middle' border='0'>&nbsp; <a href=".$java_targ.">".$java_targ ."</a><br>\n"; echo $last_dir; ls_recursive2($currentfile); } // if not a directory, then files in current directory } else { $sub_len = strlen(dirname($_SERVER['SCRIPT_FILENAME']))+1; $java_targ = substr($currentfile, $sub_len); $last_dir .= "<img src='file.png' alt='' align='middle' border='0'>&nbsp; <a href=".$java_targ.">".$java_targ."</a><br>\n"; echo $last_dir; }}}} ?> <? $this = dirname($_SERVER['SCRIPT_FILENAME']); echo $this."<br>\n"; ls_recursive2($this); ?> ** ps: I can take a comment, even some criticism, but do understand: I'm fairly new at this. ---- Server IP: 82.146.33.222 Probable Submitter: 81.244.143.103 ---- Manual Page -- http://www.php.net/manual/en/function.scandir.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+61021 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+61021&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+61021&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+61021&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+61021&report=yes&reason=useless Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+61021&report=yes&reason=non-english Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+61021&report=yes&reason=already+in+docs Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+61021&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+61021&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#102314) next »