note 40976 deleted from function.ftp-rawlist by crell
| From: | crell@php.net | Date: | Thu, 31 Mar 2022 19:30:34 +0000 |
| Subject: | note 40976 deleted from function.ftp-rawlist by crell | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-217907@lists.php.net to get a copy of this message | ||
Note Submitter: naivesong at matiasmoreno dot com dot ar
----
This script will properly handle FTP servers where the returned date/time differs from the generic
mm-dd-YYYY hh:mm:ss format:
<?php
$list = ftp_rawlist ($ftp_stream, $ftp_directory);
$folders = array();
$files = array();
$links = array();
for ($i=0; $i<count($list); $i++)
{
//----convert tabs to blanks
$list[$i] = str_replace ("\t", " ", $list[$i]);
//----delete multiple blanks
while (($k = strpos($list[$i], " ")) !== FALSE)
$list[$i] = substr($list[$i],0,$k+1).trim(substr($list[$i],$k));
//----split link reference from filename where available
if (($k = strpos($list[$i], " -> ")) !== FALSE)
{
$filelink = substr($list[$i], $k+4);
$list[$i] = substr($list[$i], 0, $k);
}
else
$filelink = "";
//----parse filename
$k = strrpos($list[$i], " ");
$filename = substr($list[$i], $k+1);
$list[$i] = substr($list[$i], 0, $k);
//----parse the rest of info
list ($permissions, $list[$i]) = parsenext ($list[$i]);
list ($number, $list[$i]) = parsenext ($list[$i]);
list ($owner, $list[$i]) = parsenext ($list[$i]);
list ($group, $list[$i]) = parsenext ($list[$i]);
list ($size, $time) = parsenext ($list[$i]);
//----ok, put all this into the related array
if ($filename != "." && $filename != "..")
{
$m = array();
$m["name"] = $filename;
$m["link"] = $filelink;
$m["size"] = $size;
$m["time"] = $time;
$m["owner"] = $owner;
$m["group"] = $group;
$m["permissions"] = $permissions;
if (substr($permissions, 0, 1) == "d")
$folders[count($folders)] = $m;
else if (substr($permissions, 0, 1) == "l")
$links[count($files)] = $m;
else
$files[count($files)] = $m;
}
}
sort ($folders);
sort ($files);
sort ($links);
?>
This script works fine on any raw list that complies with the following format:
permissions number owner group size time name [" -> " link_to]
where time may have whatever format the FTP server wants. ;-)
It also returns folders, files and links in separated sorted lists (got this idea from the note of
"postmaster at alishomepage dot com" in this help page).