Skip to content

ftp_nlist()

Understanding the PHP Function ftp_nlist()

The ftp_nlist() function is a built-in PHP function that returns an array of filenames in the specified directory on the FTP server. In this article, we'll discuss the function in detail and provide you with a comprehensive guide to using it in your PHP projects.

What is ftp_nlist()?

The ftp_nlist() function is a PHP built-in function that returns an array of filenames in the specified directory on the FTP server. Note that the returned filenames do not include the directory prefix. The function takes two parameters:

  1. ftp_stream: The FTP\Connection object returned by the ftp_connect() function.
  2. directory: The directory path on the FTP server.

The function returns an array of filenames on success. Otherwise, it returns false.

Syntax of ftp_nlist()

The syntax of the ftp_nlist() function is as follows:

Syntax of ftp_nlist()

php
array ftp_nlist ( FTP\Connection $ftp_stream , string $directory )

The ftp_nlist() function takes two required parameters, ftp_stream and directory. The ftp_stream parameter is the FTP\Connection object returned by the ftp_connect() function. The directory parameter is the path to the directory on the FTP server that you want to list.

Usage of ftp_nlist()

To use the ftp_nlist() function, you first need to establish a connection to the FTP server using the ftp_connect() function. Here's an example:

Usage of ftp_nlist()

php
<?php

// Set up an FTP connection
$conn = ftp_connect('ftp.example.com');
if (!$conn) {
    die('Could not connect to FTP server.');
}

// Login with your FTP credentials
if (!ftp_login($conn, 'username', 'password')) {
    die('Login failed.');
}

// Enable passive mode (often required for directory listings)
ftp_pasv($conn, true);

// Get an array of filenames in the specified directory
$files = ftp_nlist($conn, '/public_html/');

// Output the array of filenames
print_r($files);

// Close the FTP connection
ftp_close($conn);

In this example, we establish a connection to the FTP server using the ftp_connect() function and verify it succeeded. Then we log in using our FTP credentials using the ftp_login() function and check for login success. We enable passive mode, which is frequently required for ftp_nlist() to work correctly across different FTP servers. We get an array of filenames in the specified directory using the ftp_nlist() function and output the array of filenames using the print_r() function. Finally, we close the FTP connection using the ftp_close() function.

Error handling in ftp_nlist()

It's important to handle errors properly when using the ftp_nlist() function. If the function returns false, it means that the listing was unsuccessful. Note that an empty directory returns an empty array [], not false. Here's an example of how to handle errors:

Error handling in ftp_nlist()

php
<?php

// $conn is assumed to be established from the previous example
$file_list = ftp_nlist($conn, '/public_html/');

if ($file_list === false) {
    echo "Failed to list directory on remote server.\n";
}

ftp_close($conn);

By handling errors appropriately and checking the return value of the function, you can ensure the success of your FTP operations using the ftp_nlist() function.

Conclusion

In conclusion, the ftp_nlist() function is a useful tool for listing filenames in a directory on an FTP server. With proper usage and error handling, this function can be a valuable asset in your PHP projects.

Practice

What are the important things to know about the FTP nlist function in PHP?

Dual-run preview — compare with live Symfony routes.