note 80166 deleted from function.ftp-rawlist by felipe
| From: | felipe@php.net | Date: | Thu, 03 Jan 2008 01:31:41 +0000 |
| Subject: | note 80166 deleted from function.ftp-rawlist by felipe | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-134677@lists.php.net to get a copy of this message | ||
Note Submitter: webmaster at weltvolk dot de
----
How to get the file list in alphabetical order with all directories at the top:
<?php
//1. Function to split ftp_rawlist() information array element
function parse_rawlist($array) {
foreach ($array as $v) {
$info = array();
$vinfo = preg_split("/[\s]+/", $v, 9);
if ($vinfo[0] !== "total") {
$info['chmod'] = $vinfo[0];
$info['num'] = $vinfo[1];
$info['owner'] = $vinfo[2];
$info['group'] = $vinfo[3];
$info['size'] = $vinfo[4];
$info['month'] = $vinfo[5];
$info['day'] = $vinfo[6];
$info['time'] = $vinfo[7];
$info['name'] = $vinfo[8];
$rawlist[$info['name']] = $info;
}
}
return $rawlist;
}
//2. Separation of files and folders
$ftp_rawlist = ftp_rawlist($ftp_connect, ".");
//Use parse_rawlist()
$rawlist = parse_rawlist($ftp_rawlist);
//One array for the directories, one for the files
$dir = array();
$file = array();
foreach ($rawlist as $k => $v) {
//Identify data type by the data permissions
if ($v['chmod']{0} == "d") {
$dir[$k] = $v;
} elseif ($v['chmod']{0} == "-") {
$file[$k] = $v;
}
}
//3. HTML output
echo "<table>";
//Directories
foreach ($dir as $k => $v) {
echo "<tr><td>[ $k ]</td><td>" . $v['chmod'] .
"</td><td>" . $v['owner'] . "</td><td";
if (count($file) > 0) {
echo " colspan=\"2\">";
} else {
echo ">";
}
echo "" . $v['group'] . "</td><td>" .
$v['month'] . " " . $v['day'] . " " . $v['time'] .
"</td></tr>";
}
//Files
foreach ($dir as $k => $v) {
echo "<tr><td>$k</td><td>" . $v['chmod'] .
"</td><td>" . $v['owner'] . "</td><td>" .
$v['group'] . "</td><td>" . $v['size'] . "
Byte</td><td>" . $v['month'] . " " . $v['day'] .
" " . $v['time'] . "</td></tr>";
}
echo "</table>";
?>
Result looks like:
[ a_directory ] drwxr-xr-r user nobody Dec 31 20:59
[ b_directory ] drwxr-xr-r user nobody Dec 31 20:59
[ c_directory ] drwxr-xr-r user nobody Dec 31 20:59
...
a_file.htm -rw-r--r-- user nobody 36817 Byte Dec 31 20:59
b_file.htm -rw-r--r-- user nobody 36817 Byte Dec 31 20:59
c_file.htm -rw-r--r-- user nobody 36817 Byte Dec 31 20:59
...