note 69551 added to function.ftp-rawlist
| From: | MSpreijatGMail at osu1 dot php dot net | Date: | Sun, 10 Sep 2006 00:11:44 +0000 |
| Subject: | note 69551 added to function.ftp-rawlist | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-116967@lists.php.net to get a copy of this message | ||
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)
----
Server IP: 194.109.193.119
Probable Submitter: 194.109.235.68
----
Manual Page -- http://www.php.net/manual/en/function.ftp-rawlist.php
Edit -- https://master.php.net/note/edit/69551
Del: integrated -- https://master.php.net/note/delete/69551/integrated
Del: useless -- https://master.php.net/note/delete/69551/useless
Del: bad code -- https://master.php.net/note/delete/69551/bad+code
Del: spam -- https://master.php.net/note/delete/69551/spam
Del: non-english -- https://master.php.net/note/delete/69551/non-english
Del: in docs -- https://master.php.net/note/delete/69551/in+docs
Del: other reasons-- https://master.php.net/note/delete/69551
Reject -- https://master.php.net/note/reject/69551
Search -- https://master.php.net/manage/user-notes.php