note 61021 deleted from function.scandir by felipe
| From: | felipe@php.net | Date: | Fri, 29 Aug 2008 23:24:14 +0000 |
| Subject: | note 61021 deleted from function.scandir by felipe | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-143997@lists.php.net to get a copy of this message | ||
Note Submitter: Bosmans Koen
----
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 .= " ";
}
// 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 .= " <img src='folder.png'
alt='' align='middle' border='0'>
<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'>
<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.