PHP Function Reference

PHP ftp_mlsd() Function



The PHP ftp_mlsd() function returns a list of files in the given directory.

Syntax

ftp_mlsd(ftp, directory)

Parameters

ftp Required. Specify the FTP connection to use.
directory Required. Specify the directory to be listed. Use "." to specify the current directory.

Return Value

Returns an array of arrays with file infos from the specified directory on success or false on error.

Example:

The example below shows the usage of ftp_mlsd() function.

<?php
//FTP server to use
$ftp_server = "ftp.example.com";
   
//set up a connection or die 
$ftp = ftp_connect($ftp_server)
    or die("Could not connect to $ftp_server");
   
if($ftp) {
  //trying to login
  if(@ftp_login($ftp, $ftp_user, $ftp_pass)) {

    //getting the contents of the current directory
    $contents = ftp_mlsd($ftp, '.');

    //displaying the contents
    var_dump($contents);
  } else {
    echo "Couldn't connect as $ftp_user\n";
  }

  //close the connection
  ftp_close($ftp);
}
?>

The output of the above code will be similar to:

array(3) {
  [0]=>
  array(8) {
    ["name"]=>
    string(1) "."
    ["modify"]=>
    string(14) "20171212154511"
    ["perm"]=>
    string(7) "flcdmpe"
    ["type"]=>
    string(4) "cdir"
    ["unique"]=>
    string(11) "811U5740002"
    ["UNIX.group"]=>
    string(2) "33"
    ["UNIX.mode"]=>
    string(4) "0755"
    ["UNIX.owner"]=>
    string(2) "33"
  }
  [1]=>
  array(8) {
    ["name"]=>
    string(2) ".."
    ["modify"]=>
    string(14) "20171212154511"
    ["perm"]=>
    string(7) "flcdmpe"
    ["type"]=>
    string(4) "pdir"
    ["unique"]=>
    string(11) "811U5740002"
    ["UNIX.group"]=>
    string(2) "33"
    ["UNIX.mode"]=>
    string(4) "0755"
    ["UNIX.owner"]=>
    string(2) "33"
  }
  [2]=>
  array(8) {
    ["name"]=>
    string(11) "public_html"
    ["modify"]=>
    string(14) "20171211171525"
    ["perm"]=>
    string(7) "flcdmpe"
    ["type"]=>
    string(3) "dir"
    ["unique"]=>
    string(11) "811U5740525"
    ["UNIX.group"]=>
    string(2) "33"
    ["UNIX.mode"]=>
    string(4) "0755"
    ["UNIX.owner"]=>
    string(2) "33"
  }
}

❮ PHP FTP Reference