ftp_rawlist

(PHP 4, PHP 5)

ftp_rawlistReturns a detailed list of files in the given directory

Description

array ftp_rawlist ( resource $ftp_stream , string $directory [, bool $recursive = false ] )

ftp_rawlist() executes the FTP LIST command, and returns the result as an array.

Parameters

ftp_stream

The link identifier of the FTP connection.

directory

The directory path. May include arguments for the LIST command.

recursive

If set to TRUE, the issued command will be LIST -R.

Return Values

Returns an array where each 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.

Examples

Example #1 ftp_rawlist() example

<?php

// set up basic connection
$conn_id ftp_connect($ftp_server);

// login with username and password
$login_result ftp_login($conn_id$ftp_user_name$ftp_user_pass);

// get the file list for /
$buff ftp_rawlist($conn_id'/');

// close the connection
ftp_close($conn_id);

// output the buffer
var_dump($buff);
?>

The above example will output something similar to:

array(3) {
  [0]=>
  string(65) "drwxr-x---   3 vincent  vincent      4096 Jul 12 12:16 public_ftp"
  [1]=>
  string(66) "drwxr-x---  15 vincent  vincent      4096 Nov  3 21:31 public_html"
  [2]=>
  string(73) "lrwxrwxrwx   1 vincent  vincent        11 Jul 12 12:16 www -> public_html"
}

Changelog

Version Description
4.3.0 recursive was added.

See Also

  • ftp_nlist() - Returns a list of files in the given directory

Коментарии

Автор:
If you write
<?php
rawlist 
($ftp"-a");
?>
The command will be "LIST -a", so the retuned list will also contain hidden files like ".htaccess".

In this case all files and folders of the current directory are contained.
To list another folder, you must change to it with "ftp_chdir".
2004-01-06 05:40:40
http://php5.kiev.ua/manual/ru/function.ftp-rawlist.html
NO, NO, NO.

The above examples are all wrong, the spaces given in array are not there "just because", its just a tabbed structure. In php we don't have structures like in c/cpp, but the following function will do the job.

<?php

function parse_rawlist$array ) {

    for ( 
$i 1$i count($array); $i++ ) {

       
$current $array[$i];
   
       
$structure[$i]['perms']  = substr($current010);
       
$structure[$i]['number'] = trim(substr($current113));
       
$structure[$i]['owner']  = trim(substr($current158));
       
$structure[$i]['group']  = trim(substr($current248));
       
$structure[$i]['size']   = trim(substr($current338));
       
$structure[$i]['month']  = trim(substr($current423));
       
$structure[$i]['day']    = trim(substr($current462));
       
$structure[$i]['time']   = substr($current495);
       
$structure[$i]['name']   = substr($current55strlen($current) - 55);

    }

    return 
$structure;

}

?>
2004-09-01 17:06:32
http://php5.kiev.ua/manual/ru/function.ftp-rawlist.html
Автор:
Note that there is no standard for the format, therefore don't be suprised when parsing routines for this work perfectly on some servers, and fail horribly on some.
2004-10-22 22:53:46
http://php5.kiev.ua/manual/ru/function.ftp-rawlist.html
The previous regular expression(by Jonathan Almarez,ergye at yahoo dot com and guru at virusas dot lt) is very good.But i found it does not  take into account for directories(number>9)

Change [0-9] to [0-9]*

The code below not only parses:
drwxrwxr-x     9 msik     ia           4096 Nov  5 14:19 Group3

It also parses:
drwxrwxr-x   19 msik     ia           4096 Nov  5 14:19 Group3

drwxrwxr-x   119 msik     ia           4096 Nov  5 14:19 Group3


0 = file
1 = directory
2 = simlink
<?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]{2}:[0-9]{2})|[0-9]{4}) (.+)"$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['time'] = $regs[7];
           
$tmp_array['name'] = $regs[9];
       }
       
$dir_list[] = $tmp_array;
   }
   return 
$dir_list;
}
$buff ftp_rawlist($cid"/");
$items itemize_dir($buff);

?>
2004-11-26 00:55:30
http://php5.kiev.ua/manual/ru/function.ftp-rawlist.html
list all (including hidden files and dirs):
<?php
  $contents 
ftp_rawlist($conn_id"-al ".$dir_name);
?>

just as ftp command:
LIST al 

"-aF " is equal to '-al', please refer to "ls --help"
2004-12-20 01:58:46
http://php5.kiev.ua/manual/ru/function.ftp-rawlist.html
Excelent expresion, but don't match SUID, SGUI and Sticky flags when 'x' is disabled. Fix it with [rwxstST-].

<?php

function itemize_dir($contents) {
   foreach (
$contents as $file) {
       if(
ereg("([-dl][rwxstST-]+).* ([0-9]*) ([a-zA-Z0-9]+).* ([a-zA-Z0-9]+).* ([0-9]*) ([a-zA-Z]+[0-9: ]*[0-9])[ ]+(([0-9]{2}:[0-9]{2})|[0-9]{4}) (.+)"$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['time'] = $regs[7];
           
$tmp_array['name'] = $regs[9];
       }
       
$dir_list[] = $tmp_array;
   }
   return 
$dir_list;
}
$buff ftp_rawlist($cid"/");
$items itemize_dir($buff);

?>
2005-01-11 07:44:11
http://php5.kiev.ua/manual/ru/function.ftp-rawlist.html
When you try:

$path = "directory pathname with spaces";
$list = ftp_rawlist($conn_id,$path);

It doesn't work

but when you try:

$path = "directory pathname with spaces";
ftp_chdir($conn_id,$path);
$list = ftp_rawlist($conn_id,".");

It works
2005-02-03 04:57:58
http://php5.kiev.ua/manual/ru/function.ftp-rawlist.html
this snip fixes the date problem with the listing and sorts out the variables:

$filedata['access_permissions']
$filedata['link_count']
$filedata['uid']
$filedata['gid']
$filedata['size']
$filedata['mod_date_month']
$filedata['mod_date_day']
$filedata['mod_time']
$filedata['name']

list($filedata['access_permissions'], $filedata['link_count'], $filedata['uid'], $filedata['gid'], $filedata['size'], $filedata['mod_date_month'], $filedata['mod_date_day'], $filedata['mod_time'], $filedata['name']) = preg_split("/[\s,]+/", $value);

$filedata['type'] = $filedata['access_permissions']{0};

$filedata['access_permissions'] = substr($filedata['access_permissions'],1);

// now check the date to see if the last modifcation was this year or last.

if ( strrpos($filedata['mod_time'], ':') != 2 ) { $filedata['mod_date'] = $filedata['mod_date_month'] ." " . $filedata['mod_date_day'] . " " . $filedata['mod_time']; $filedata['mod_time'] = "00:00"; } else { $filedata['mod_date'] = $filedata['mod_date_month'] ." " . $filedata['mod_date_day'] . " " . date("Y"); }
2005-05-07 15:41:25
http://php5.kiev.ua/manual/ru/function.ftp-rawlist.html
I was having problems picking up the directories on a mac using cjacobsen at pge dot cl's (11-Jan-05) solution. Slight ammendment to the regexp works for me:

ereg("([-dl][rwxstST-]+).* ?([0-9 ]* )?([a-zA-Z0-9]+).* ([a-zA-Z0-9]+).* ([0-9]*) ([a-zA-Z]+[0-9: ]*[0-9])[ ]+(([0-9]{2}:[0-9]{2})|[0-9]{4}) (.+)", $file, $regs)
2005-12-03 16:38:51
http://php5.kiev.ua/manual/ru/function.ftp-rawlist.html
This is a little cleaner:

function parse_rawlist( $array )
{
    foreach($array as $curraw)
    {
        $struc = array();
        $current = preg_split("/[\s]+/",$curraw,9);

        $struc['perms']  = $current[0];
        $struc['number'] = $current[1];
        $struc['owner']  = $current[2];
        $struc['group']  = $current[3];
        $struc['size']  = $current[4];
        $struc['month']  = $current[5];
        $struc['day']    = $current[6];
        $struc['time']  = $current[7];
        $struc['year']  = $current[8];
        $struc['raw']  = $curraw;
        $structure[$struc['name']] = $struc;
    }
   return $structure;

}
2006-10-17 04:24:24
http://php5.kiev.ua/manual/ru/function.ftp-rawlist.html
Автор:
With this handy function based on functions presented here you get the file list in alphabetical order with all directories on top:

<?php
function rawlist_dump() {
  global 
$ftp_connect;
 
$ftp_rawlist ftp_rawlist($ftp_connect".");
  foreach (
$ftp_rawlist as $v) {
   
$info = array();
   
$vinfo preg_split("/[\s]+/"$v9);
    if (
$vinfo[0] !== "total") {
     
$info['chmod'] = $vinfo[0];
     
$info['num'] = $vinfo[1];
     
$info['owner'] = $vinfo[2];
     
$info['group'] = $vinfo[3];
     
$info['size'] = $vinfo[4];
     
$info['month'] = $vinfo[5];
     
$info['day'] = $vinfo[6];
     
$info['time'] = $vinfo[7];
     
$info['name'] = $vinfo[8];
     
$rawlist[$info['name']] = $info;
    }
  }
 
$dir = array();
 
$file = array();
  foreach (
$rawlist as $k => $v) {
    if (
$v['chmod']{0} == "d") {
     
$dir[$k] = $v;
    } elseif (
$v['chmod']{0} == "-") {
     
$file[$k] = $v;
    }
  }
  foreach (
$dir as $dirname => $dirinfo) {
      echo 
"[ $dirname ] " $dirinfo['chmod'] . " | " $dirinfo['owner'] . " | " $dirinfo['group'] . " | " $dirinfo['month'] . " " $dirinfo['day'] . " " $dirinfo['time'] . "<br>";
  }
  foreach (
$file as $filename => $fileinfo) {
      echo 
"$filename " $fileinfo['chmod'] . " | " $fileinfo['owner'] . " | " $fileinfo['group'] . " | " $fileinfo['size'] . " Byte | " $fileinfo['month'] . " " $fileinfo['day'] . " " $fileinfo['time'] . "<br>";
  }
}
rawlist_dump();
?>
2008-01-04 08:17:21
http://php5.kiev.ua/manual/ru/function.ftp-rawlist.html
Regarding converting permissions from symbolic notation to octal, note that Hazem dot Khaled at gmail dot com's chmodnum function produces INCORRECT results.  The resutls are base-10 numbers that only LOOK like they are octal numbers.  The function also ignores setuid, setgid and sticky bits, and will produce incorrect numbers if such a file is encountered.  Instead, this brute-force code works.  Maybe there is something more slick, but this isn't too CPU-intensive (note that it assumes you've error-checked that you indeed have a 10-character string!):

      $permissions = 'drwxr-xr-x';  // or whatever
      $mode = 0;

      if ($permissions[1] == 'r') $mode += 0400;
      if ($permissions[2] == 'w') $mode += 0200;
      if ($permissions[3] == 'x') $mode += 0100;
      else if ($permissions[3] == 's') $mode += 04100;
      else if ($permissions[3] == 'S') $mode += 04000;

      if ($permissions[4] == 'r') $mode += 040;
      if ($permissions[5] == 'w') $mode += 020;
      if ($permissions[6] == 'x') $mode += 010;
      else if ($permissions[6] == 's') $mode += 02010;
      else if ($permissions[6] == 'S') $mode += 02000;

      if ($permissions[7] == 'r') $mode += 04;
      if ($permissions[8] == 'w') $mode += 02;
      if ($permissions[9] == 'x') $mode += 01;
      else if ($permissions[9] == 't') $mode += 01001;
      else if ($permissions[9] == 'T') $mode += 01000;

      printf('Mode is %d decimal and %o octal', $mode, $mode);
2008-04-24 08:13:58
http://php5.kiev.ua/manual/ru/function.ftp-rawlist.html
Автор:
Some FTP servers only allow you to get list of files under current working directory. So if you always get result as empty array (array(0){ }), try changing the cwd befor get the list:

<?php
function ftprawlist($connid$dir) {
 
ftp_chdir($connid$dir);
  return 
ftp_rawlist($connid"-a");
}
?>
2008-11-07 04:43:29
http://php5.kiev.ua/manual/ru/function.ftp-rawlist.html
Автор:
Why not using POSIX regex to do the job here ? A preg_replace returns the information in an associative array with the following keys:

<rights>
<number>
<owner>
<group>
<file_size>
<mod_time>
<file>
<type> * that's a bonus: it will tell you if the item is either a file or a directory

Code is shown below:

$list=@ftp_rawlist($con,$directory) ;

$items=array() ;

foreach($list as $_)
preg_replace(

'`^(.{10}+)(\s*)(\d{1})(\s*)(\d*|\w*)'.
'(\s*)(\d*|\w*)(\s*)(\d*)\s'.
'([a-zA-Z]{3}+)(\s*)([0-9]{1,2}+)'.
'(\s*)([0-9]{2}+):([0-9]{2}+)(\s*)(.*)$`Ue',

'$items[]=array(
"rights"=>"$1", 
"number"=>"$3", 
"owner"=>"$5", "group"=>"$7",
"file_size"=>"$9",
"mod_time"=>"$10 $12 $14:$15",
"file"=>"$17",
"type"=>print_r((preg_match("/^d/","$1"))?"dir":"file",1));',

$_) ; # :p
2009-03-12 12:47:08
http://php5.kiev.ua/manual/ru/function.ftp-rawlist.html
Автор:
To format the _recrusive_ result of this function I use this:

<?php
$conn_id 
ftp_connect(FTP_SERVER);

$login_result ftp_login($conn_idFTP_USRFTP_PASS);

$rawfiles ftp_rawlist($conn_id'/'true);

ftp_close($conn_id);

// here the magic begins!
$structure = array();
$arraypointer = &$structure;
foreach (
$rawfiles as $rawfile) {
    if (
$rawfile[0] == '/') {
       
$paths array_slice(explode('/'str_replace(':'''$rawfile)), 1);
       
$arraypointer = &$structure;
        foreach (
$paths as $path) {
            foreach (
$arraypointer as $i => $file) {
                if (
$file['text'] == $path) {
                   
$arraypointer = &$arraypointer$i ]['children'];
                    break;
                }
            }
        }
    } elseif(!empty(
$rawfile)) {
       
$info preg_split("/[\s]+/"$rawfile9);       
       
$arraypointer[] = array(
           
'text'   => $info[8],
           
'isDir'  => $info[0]{0} == 'd',
           
'size'   => byteconvert($info[4]),
           
'chmod'  => chmodnum($info[0]),
           
'date'   => strtotime($info[6] . ' ' $info[5] . ' ' $info[7]),
           
'raw'    => $info
           
// the 'children' attribut is automatically added if the folder contains at least one file
       
);
    }
}

// in $structure is all the data
print_r($structure);

// little helper functions
function byteconvert($bytes) {
   
$symbol = array('B''KB''MB''GB''TB''PB''EB''ZB''YB');
   
$exp floorlog($bytes) / log(1024) );
    return 
sprintf'%.2f ' $symbol$exp ], ($bytes pow(1024floor($exp))) );
}

function 
chmodnum($chmod) {
   
$trans = array('-' => '0''r' => '4''w' => '2''x' => '1');
   
$chmod substr(strtr($chmod$trans), 1);
   
$array str_split($chmod3);
    return 
array_sum(str_split($array[0])) . array_sum(str_split($array[1])) . array_sum(str_split($array[2]));
}

?>
2009-05-26 07:20:09
http://php5.kiev.ua/manual/ru/function.ftp-rawlist.html
this is function to check for dirs 

<?php
function ftp_isdir($connect_id,$dir)
{
    if(
ftp_chdir($connect_id,$dir))
    {
       
ftp_cdup($connect_id);
        return 
true;

    }
    else
    {
        return 
false;
    }
}
?>
2009-06-23 22:38:14
http://php5.kiev.ua/manual/ru/function.ftp-rawlist.html
ftp_rawlist kept returning empty file listing, it would work on some machines but not others, it turned out to be ftp_pasv command was needed.
Very frustrating
2009-11-03 10:02:09
http://php5.kiev.ua/manual/ru/function.ftp-rawlist.html
Автор:
There are a couple of php-related reasons given here for ftp_rawlist returning an empty result. However be aware that ZoneAlarm (and possibly other) firewalls can block responses without giving any visible clue so be sure to check that first.
2010-05-02 19:46:46
http://php5.kiev.ua/manual/ru/function.ftp-rawlist.html
Get a listing of all files including hidden files except '.' or '..' use:

<?php
ftp_chdir
($connid$dir);
ftp_rawlist($connid"-A");
?>

This had me dancing in circles for some time!
2010-05-13 07:02:55
http://php5.kiev.ua/manual/ru/function.ftp-rawlist.html
Автор:
In case anybody wants to get a detailed listing using the MLSD command over a passive connection, the following function might be helpful as a starting point for your own implementation:

<?php
function ftp_mlsd($ftp$directory) {
   
$ok = @ftp_chdir($ftp$directory);
    if (!
$ok) {
        return 
false;
    }
   
$ret ftp_raw($ftp'PASV');
    if (
preg_match(
       
'/^227.*\(([0-9]+,[0-9]+,[0-9]+,[0-9]+),([0-9]+),([0-9]+)\)$/',
       
$ret[0], $matches)) {
       
$controlIP str_replace(',''.'$matches[1]);
       
$controlPort intval($matches[2])*256+intval($matches[3]);
       
$socket fsockopen($controlIP$controlPort);
       
ftp_raw($ftp'MLSD');
       
$s '';
        while (!
feof($socket)) {
           
$s .= fread($socket4096);
        }
       
fclose($socket);
       
$files = array();
        foreach (
explode("\n"$s) as $line) {
            if (!
$line) {
                continue;
            }
           
$file = array();
            foreach (
explode(';'$line) as $property) {
                list(
$key$value) = explode('='$property);
                if (
$value) {
                   
$file[$key] = $value;
                } else {
                   
$filename trim($key);
                }
            }
           
$files[$filename] = $file;
        }
        return 
$files;
    }
    return 
false;
}
?>

Please note that this function ignores the setting of ftp_pasv(). Making the function to work universally for both active and passive connections is left as an exercise to the reader ;-)
2010-11-24 08:18:22
http://php5.kiev.ua/manual/ru/function.ftp-rawlist.html
Here's a simple function that'll parse the data returned by ftp_rawlist() into an associative array. I wrote it because some of the functions listed below are way to long, complex or won't work with file names that contain spaces.

<?php
   
function listDetailed($resource$directory '.') {
        if (
is_array($children = @ftp_rawlist($resource$directory))) {
           
$items = array();

            foreach (
$children as $child) {
               
$chunks preg_split("/\s+/"$child);
                list(
$item['rights'], $item['number'], $item['user'], $item['group'], $item['size'], $item['month'], $item['day'], $item['time']) = $chunks;
               
$item['type'] = $chunks[0]{0} === 'd' 'directory' 'file';
               
array_splice($chunks08);
               
$items[implode(" "$chunks)] = $item;
            }

            return 
$items;
        }

       
// Throw exception or return false < up to you
   
}
?>
2012-12-10 21:12:19
http://php5.kiev.ua/manual/ru/function.ftp-rawlist.html
All  parse_rawlist Functions here have one Problem.

when a file starts with a space character like " robots.txt ", it will be ignored.

Rename, delete will fail...
2014-03-14 12:55:01
http://php5.kiev.ua/manual/ru/function.ftp-rawlist.html
The solution of fredvanetten at tinqle dot com is nice but needs further evaluation as because of the preg_split and static listing of the variables will produce different values: comparing a file of today or an older, from a previous year:

Array
(
    [time] => 2012
    [day] => 11
    [month] => Sep
    [size] => 37262
    [group] => group
    [user] => owner
    [number] => 1
    [rights] => -rw-rw-rw-
)

Array
(
    [time] => 14:01
    [day] => 23
    [month] => Apr
    [size] => 37262
    [group] => group
    [user] => owner
    [number] => 1
    [rights] => -rw-rw-rw-
)
2014-04-24 22:17:19
http://php5.kiev.ua/manual/ru/function.ftp-rawlist.html
Note that this function also will return false if the content of the provided directory is empty.
2018-04-19 20:26:24
http://php5.kiev.ua/manual/ru/function.ftp-rawlist.html

    Поддержать сайт на родительском проекте КГБ