PHP Function Reference

PHP ftp_nlist() Function



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

Syntax

ftp_nlist(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. This parameter can also include arguments, eg. ftp_nlist($ftp, "-la /your/dir");. Note that this parameter isn't escaped.

Return Value

Returns an array of filenames from the specified directory on success or false on error.

Example:

The example below shows the usage of ftp_nlist() 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 filenames of the current directory
    $filelist = ftp_nlist($ftp, '.');

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

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

The output of the above code will be:

Array
(
    [0] => "code.txt"
    [1] => "error.txt"
    [2] => "input.txt"
)

❮ PHP FTP Reference