note 46182 added to function.ftp-rawlist

From: Date: Fri, 01 Oct 2004 15:28:52 +0000
Subject: note 46182 added to function.ftp-rawlist
Groups: php.notes 
Request: Send a blank email to php-notes+get-77686@lists.php.net to get a copy of this message
I've gently modified the code put forward by the last two posters - an explanation follows the code: <?php function itemize_dir($contents) { foreach ($contents as $file) { if(ereg("([-dl][rwxst-]+).* ([0-9])* ([a-zA-Z0-9]+).* ([a-zA-Z0-9]+).* ([0-9]*) ([a-zA-Z]+[0-9: ]*[0-9]) +([0-9]{4})*([0-9]{2}:[0-9]{2})* (.+)", $file, $regs)) { $type = (int) strpos("-dl", $regs[1]{0}); $tmp_array['line'] = $regs[0]; $tmp_array['type'] = $type; $tmp_array['rights'] = $regs[1]; $tmp_array['number'] = $regs[2]; $tmp_array['user'] = $regs[3]; $tmp_array['group'] = $regs[4]; $tmp_array['size'] = $regs[5]; $tmp_array['date'] = date("m-d",strtotime($regs[6])); $tmp_array['year'] = $regs[7]; $tmp_array['time'] = $regs[8]; $tmp_array['name'] = $regs[9]; } $dir_list[] = $tmp_array; } return $dir_list; } ?> This script attempts to compensate for two conditions that I found while testing the last poster's (wonderful) script on my FTP server (running PureFTP). Here are examples: drwxr-x--- 19 585 99 4096 Sep 30 23:43 public_html In this case, the previous script will fail at the number 19 - here called the 'number' - because it doesn't expect to see more than a single digit. drwxr-xr-x 3 585 fan 4096 Oct 31 2003 public_ftp In this case, the previous script fails when it expects to see the file modification /time/ but instead, sees an extra space followed by the file modification /year/. On my FTP server, files that have not been modified within the current year do not return a file modification time. I don't know if this is peculiar to PureFTP or not. In my amendment to this script, there is a new 'year' category which is empty if no year is given. Conversely, if the year is given but not the modification time is, then 'time' is empty. This is important to note if you are using the original script and are depending on 'time' to hold a value. I can't promise that this version won't fail under conditions where the previous script succeeded - not having access to an infinite variety of FTP servers to test on. I have tested it on a dreamhost server (running ProFTPD 1.2.5rc1) and it appears to work though neither of the above conditions are currently present on that server. I'm posting it, really, in order not to be ungenerous and simply point out the problems with the original script. I'm sure that someone with more advanced regex skills will be able to improve upon it. ---- Manual Page -- http://www.php.net/manual/en/function.ftp-rawlist.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+46182 Delete -- http://master.php.net/manage/user-notes.php?action=delete+46182&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+46182&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#77686) next »