downloads | documentation | faq | getting help | mailing lists | reporting bugs | sites | links | my 
search for in the  
view the version of this page
Last updated: Thu, 21 Aug 2003


(PHP 3>= 3.0.13, PHP 4 )

ftp_rawlist -- Returns a detailed list of files in the given directory


array ftp_rawlist ( resource ftp_stream, string directory)

ftp_rawlist() executes the FTP LIST command, and returns the result as an array. Each array element corresponds to one line of text. The output is not parsed in any way. The system type identifier returned by ftp_systype() can be used to determine how the results should be interpreted.

See also ftp_nlist().

add a note add a note User Contributed Notes
postmaster at alishomepage dot com
19-Oct-2003 07:39
Here we go for a 100% working code... :D



substr($str,0,1)==" "){$str=substr($str,1);}


$permissions,$next)=split(" ",$list[$i],2);
$num,$next)=split(" ",cutspaces($next),2);
$owner,$next)=split(" ",cutspaces($next),2);
$group,$next)=split(" ",cutspaces($next),2);
$size,$next)=split(" ",cutspaces($next),2);
$month,$next)=split(" ",cutspaces($next),2);
$day,$next)=split(" ",cutspaces($next),2);
$year_time,$filename)=split(" ",cutspaces($next),2);
$filename!="." && $filename!=".."){
        } else {



so this will simply "get" all the information WITHOUT being in any case interfered with some spaces, ... etc etc... It will even put files in a $files array and folders in a $folders array, and sort them, so you will be able of using all this later

and: the "folders" will NOT contain "." and ".." ;)

so you can use all this to make a beautiful FTP interface... later on you could for example put permissions and etc etc in other arrays to use them in your result... cute....
postmaster at alishomepage dot com
19-Oct-2003 07:05
Another "formula" for decoding the rawlist: the ide is that normaly a "ls" contains info like below:

<file rights> <number> <owner> <group> <date> <time> <filename>

and: the "time" contains a ":" (which is NOT contained in any of the other infos -if we reversely read the array)

and, in the case the "time" would not exist, i have done a "protection script" that would try to find out using the year... Yet that may NOT always work since a user name or group name MAY contain an item like "1999" etc... (and i did not check the date format reversely -on the filename first- since a probability of presence is higher of a date is MUCH higher in a filename)

So here we go:


[... code ...]


code ...]


code ...]


and $elmt would *normally* contain the info we want... but i'm still working on it!
Svenne Burner
02-Aug-2003 08:12
If you want to use explode, for all files in a dir, or something..


$rawlist[$i][$x] = ereg_replace( "([\x20]+)", "\x20", $rawlist[$i][$x]);

$rawlist[$i][$x] = explode("\x20", $rawlist[$i][$x]);

echo $rawlist[$i][$x][$k] . "<br>";

The output prints a different amount of spaces.
jmv AT jmvware DOT com
15-May-2003 04:27
Seems it doesn't have to be so complicated:

$dirline=ereg_replace(" +",' ',$dirline);
if (ereg('^d',$dirline)) $isdir=1;
,$size,$month,$day,$year_time,$name)=split(' ',$dirline);
kioob at wanadoo dot fr
11-Sep-2002 05:02
My project is to help users to use ftp functions with PHP. So I made a complet class for that :

Enjoy !
murasame at danzers dot org
03-Jun-2002 08:11
I tweaked the regexp above and used it like this in a for-loop:

ereg("([-d])([rwxst-]{9}).* ([0-9]*) ([a-zA-Z]+[0-9: ]* [0-9]{2}:?[0-9]{2}) (.+)", $raw[$i], $regs)

$out[] = array("is_dir" =>
  ($regs[1] == "d") ? true : false,
 "mod" => $regs[2],
 "size" => $regs[3],
 "time" => $regs[4],
 "name" => $regs[5],
 "raw" => $regs[0]);

the previous version couldn't handle old dates which are in format 'M d Y' instead of 'M d H:i' correctly
maikdemaSPAM at yahooMAPS dot com
28-Mar-2002 07:27
L8 ereg mask (directory?, size, name):

ereg("([-d])[rwxst-]{9}.* ([0-9]*) [a-zA-Z]+ [0-9: ]*[0-9]

I've tried this, and it works fine if the name of the file has not a number in the first letter. for example, if I have a file named "1file.exe", it shows "file.exe"; and if I've a file named "010102.exe" it shows ".exe" only.
I've improved it by doing this:

if(ereg("([-d])[rwxst-]{9}.* ([0-9]*) ([a-zA-Z]+[0-9: ]*[0-9]) ([0-9]{2}:[0-9]{2}) (.+)",$dirline,$regs))
            if($regs[1]=="d") $dirinfo[0] = 1;
            $dirinfo[1] = $regs[2];
            $dirinfo[2] = $regs[3];
            $dirinfo[3] = $regs[4];
            $dirinfo[4] = $regs[5];
$dirinfo[0] => is a directory? (1=yes)
$dirinfo[1] => file size
$dirinfo[2] => file date
$dirinfo[3] => file time
$dirinfo[4] => file name

and now works with all filename formats!
ryan at wonko dot com
21-Mar-2002 11:32
[[ Editors note: Seems it was fixed in PHP 4.3.0 ]]
A note to developers using PHP on Windows servers: as of PHP 4.1.2, ftp_rawlist() is broken in the Windows build of PHP. It will return nothing at all, even if the same code works fine on UNIX. So if you're going crazy trying to figure out why the function isn't returning anything, stop wasting your time, you're not doing anything wrong. Hopefully this will get fixed in future versions, although it's apparently been an issue since at least 4.0.6.

More info on this bug is at
cialdi at firenze dot net
29-Jun-2001 11:24
the second parameter accepts also standard arguments of /bin/ls command like "-l" or "-t"
php at smintman dot com
26-May-2001 08:35
An example script for rekursiv directory analysis ... interesting are the ereg functions for spliting the content based on the system type...

function analysedir($dirline)
    global $systyp,$ftp_server,$stop;
    if(ereg("([-dl])[rwxst-]{9}",substr($dirline,0,10))) {
        $systyp = "UNIX";
    if(substr($dirline,0,5) == "total") {
        $dirinfo[0] = -1;
    } elseif($systyp=="Windows_NT") {
        if(ereg("[-0-9]+ *[0-9:]+[PA]?M? +<DIR> {10}(.*)",$dirline,$regs)) {
            $dirinfo[0] = 1;
            $dirinfo[1] = 0;
            $dirinfo[2] = $regs[1];
        } elseif(ereg("[-0-9]+ *[0-9:]+[PA]?M? +([0-9]+) (.*)",$dirline,$regs)) {
            $dirinfo[0] = 0;
            $dirinfo[1] = $regs[1];
            $dirinfo[2] = $regs[2];
    } elseif($systyp=="UNIX") {
        if(ereg("([-d])[rwxst-]{9}.* ([0-9]*) [a-zA-Z]+ [0-9: ]*[0-9] (.+)",$dirline,$regs)) {
            if($regs[1]=="d")    $dirinfo[0] = 1;
            $dirinfo[1] = $regs[2];
            $dirinfo[2] = $regs[3];
    if(($dirinfo[2]==".")||($dirinfo[2]=="..")) $dirinfo[0]=0;

    // array -> 0 = switch, directory or not
    // array -> 1 = filesize (if dir =0)
    // array -> 2 = filename or dirname
    return $dirinfo;

function rekdir($dir)
    global $conn_id,$filetyps,$exectyps,$ftp_server,$banlist,$size,$ssize;

    echo "<b>$dir</b><br>";
    $dirlist = ftp_rawlist($conn_id,"");

    for($i=0;$i<count($dirlist);$i++) {
        $dirinfo = analysedir($dirlist[$i]);
        if($dirinfo[0]==1) {
            $newdir = "$dir/$dirinfo[2]";
            if(($dirinfo[2]=="~")||(substr($dirinfo[2],0,1)==" "))
            else    $chdir=ftp_chdir($conn_id,$dirinfo[2]);
            $stop = 0;
//            for($k=0;($k<count($banlist))&&(!$stop);$k++) {
//                if(strpos($newdir,$banlist[$k])!=0) $stop=1;
//            }
            if(!$stop && $chdir) {
            if(!ftp_chdir($conn_id,$dir))    ftp_cdup($conn_id);
        } elseif($dirinfo[0]==0) {
            echo "$dirinfo[2]<br>";
            $size += $dirinfo[1];


$conn_id = ftp_connect($ftp_server);
$login_result = @ftp_login($conn_id, $user, $password);

if ($login_result) {
    $real_systyp = ftp_systype($conn_id);
    $systyp = $real_systyp;
    echo "scanning... $systyp<br>";



 Last updated: Thu, 21 Aug 2003
show source | credits | sitemap | mirror sites 
Copyright © 2001-2003 The PHP Group
All rights reserved.
This mirror generously provided by:
Last updated: Sat 01 Nov 2003 04:13:36 EST EST