PHP 8.2.0 RC7 available for testing

ftp_rawlist

(PHP 4, PHP 5, PHP 7, PHP 8)

ftp_rawlistBelirtilen dizindeki dosyaların ayrıntılı bir listesini döndürür

Açıklama

ftp_rawlist(FTP\Connection $ftp, string $dizin, bool $ardışık = false): array|false

ftp_rawlist() işlevi FTP LIST komutunu çalıştırıp sonuçları bir dizi içinde döndürür.

Bağımsız Değişkenler

ftp

Bir FTP\Connection nesnesi.

dizin

İçeriği listelenecek dizinin yolu. LIST için bağımsız değişkenler içerebilir.

ardışık

true belirtilirse LIST -R komutu çalıştırılır ve listeleme için alt dizinlere inilir.

Dönen Değerler

Elemanları komuta verilen yanıt metninin bir satırına karşılık gelen bir dizi döner. Belirtilen dizin geçersizse false döner.

Çıktı herhangi bir şekilde çözümlenmez. ftp_systype() işleviyle döndürülen sistem türü betimleyicisi sonuçların nasıl yorumlanacağına karar vermek için yararlı olabilir.

Sürüm Bilgisi

Sürüm: Açıklama
8.1.0 ftp bağımsız değişkeni artık FTP\Connection nesnesi kabul ediyor; evvelce ftp özkaynağı kabul ediyordu.

Örnekler

Örnek 1 - ftp_rawlist() örneği

<?php

// FTP sunucusuna bağlanalım
$ftp ftp_connect('ftp.example.com');
if (!
$ftp) die('ftp.example.com ile bağlantı kurulamadı');

// kullanıcı adı ve parola ile oturum açalım
$login_result ftp_login($ftp$ftp_user_name$ftp_user_pass);

// / için dosya listesini alalım
$buff ftp_rawlist($ftp'/');

// bağlantıyı kapatalım
ftp_close($ftp);

// tamponu çıktılayalım
var_dump($buff);
?>

Yukarıdaki örnek şuna benzer bir çıktı üretir:

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

Ayrıca Bakınız

  • ftp_nlist() - Belirtilen dizindeki dosyaların listesini döndürür
  • ftp_mlsd() - Belirtilen dizindeki dosyaların bir listesini döndürür

add a note

User Contributed Notes 24 notes

up
40
fredvanetten at tinqle dot com
9 years ago
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($chunks, 0, 8);
               
$items[implode(" ", $chunks)] = $item;
            }

            return
$items;
        }

       
// Throw exception or return false < up to you
   
}
?>
up
3
shaun at shaunfreeman dot co dot uk
12 years ago
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!
up
3
weltvolk
14 years ago
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]+/", $v, 9);
    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();
?>
up
7
mediaboat at gmail dot com
8 years ago
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...
up
2
Johnny Doe
12 years ago
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($socket, 4096);
        }
       
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 ;-)
up
0
Roline88 at hotmail dot com
4 years ago
Note that this function also will return false if the content of the provided directory is empty.
up
2
paul maybe at squirrel mail org
14 years ago
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);
up
1
admin at mikeholloway dot co dot uk
16 years ago
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)
up
0
John
12 years ago
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.
up
0
nospam at advancedonsite dot com
13 years ago
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
up
1
marc dot peterlongo at francetelecom dot com
17 years ago
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
up
0
ahmadmarafa at gmail dot com
13 years ago
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;
    }
}
?>
up
0
tmp at gmx dot de
13 years ago
To format the _recrusive_ result of this function I use this:

<?php
$conn_id
= ftp_connect(FTP_SERVER);

$login_result = ftp_login($conn_id, FTP_USR, FTP_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]+/", $rawfile, 9);       
       
$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 = floor( log($bytes) / log(1024) );
    return
sprintf( '%.2f ' . $symbol[ $exp ], ($bytes / pow(1024, floor($exp))) );
}

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

?>
up
0
francois frao_o
13 years ago
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
up
0
www.bosssftp.com
14 years ago
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");
}
?>
up
0
ari at bs-inc dot com
16 years ago
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;

}
up
0
nate at 8networks dot com
17 years ago
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"); }
up
-2
nickman at freemail dot hu
8 years ago
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-
)
up
0
cjacobsen at pge dot cl
17 years ago
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);

?>
up
0
greg at upsdn dot net
17 years ago
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"
up
0
greg at upsdn dot net
18 years ago
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
3
drwxrwxr-x   119 msik     ia           4096 Nov  5 14:19 Group3
3

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);

?>
up
0
Anonymous
18 years ago
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.
up
0
Anonymous
18 years ago
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".
up
-1
eros at speedy dot com dot ar
18 years ago
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($current, 0, 10);
       
$structure[$i]['number'] = trim(substr($current, 11, 3));
       
$structure[$i]['owner']  = trim(substr($current, 15, 8));
       
$structure[$i]['group']  = trim(substr($current, 24, 8));
       
$structure[$i]['size']   = trim(substr($current, 33, 8));
       
$structure[$i]['month']  = trim(substr($current, 42, 3));
       
$structure[$i]['day']    = trim(substr($current, 46, 2));
       
$structure[$i]['time']   = substr($current, 49, 5);
       
$structure[$i]['name']   = substr($current, 55, strlen($current) - 55);

    }

    return
$structure;

}

?>
To Top