note 69551 deleted from function.ftp-rawlist by crell
| From: | crell@php.net | Date: | Thu, 31 Mar 2022 19:18:43 +0000 |
| Subject: | note 69551 deleted from function.ftp-rawlist by crell | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-217900@lists.php.net to get a copy of this message | ||
Note Submitter: MSpreij at GMail
----
The following was inspired by a few others here, ofcourse ;-)
Works for filenames with spaces (leading, trailing, consecutive and what not), formats the date a
little taking year/time into account, leaves out items not in the $filetypes array below, and
returns a nice nested assoc array:
<?php
$filetypes = array('-'=>'file', 'd'=>'directory',
'l'=>'link');
$c = ftp_connect('localhost');
ftp_login($c, 'jackcrow', 'banshee');
$data = ftp_rawlist($c, '/users/jackcrow');
foreach($data as $line) {
if (substr(strtolower($line), 0, 5) == 'total') continue; # first line, skip it
preg_match('/'. str_repeat('([^\s]+)\s+', 7) .'([^\s]+) (.*)/',
$line, $matches); # Here be Dragons
list($permissions, $children, $owner, $group, $size, $month, $day, $time, $name) =
array_slice($matches, 1);
# if it's not a file, directory or link, I don't really care to know about it :-) comment
out the next line if you do
if (! in_array($permissions[0], array_keys($filetypes))) continue;
$type = $filetypes[$permissions[0]];
$date = date('d/m/y H:i', (strpos($time, ':') ? mktime(substr($time, 0, 2),
substr($time, -2), 0, $month, $day) : mktime(0,0,0,$month, $day, $time) ) );
$files[$name] = array('type'=>$type, 'permissions'=>substr($permissions,
1), 'children'=>$children, 'owner'=>$owner, 'group'=>$group,
'size'=>$size, 'date'=>$date);
}
print_r($files);
?>
(I'm using a pretty simple regex pattern (non-space-stuff, whitespace, non-space-stuff etc),
but amazingly /it still works!/ K.I.S.S.! The time part in the pattern is done separately to make
sure only 1 space is taken out before the filename, which might start with spaces for all you know)