PHP | ftp_nlist() function

The ftp_nlist() function is an inbuilt function in PHP which is used to get the list of all the filename and sub-directory in a specific directory on FTP server.
Syntax:
ftp_nlist( $ftp_connection, $directory );
Parameters: This function accepts two parameters as mentioned above and described below:
- $ftp_connection: It is required parameter. It specifies the already existing FTP connection to get the list of all files and sub-directory of a directory in FTP server.
- $directory: It is required parameter of String type. It specifies the directory in remote server i.e. FTP server whose file & sub-directory is to list.
Return Value: It returns array of file names and sub-directory on success and false on failure.
Note:
- This function is available for PHP 4.0.0 and newer version.
- The following examples cannot be run on online IDE. So try to run in some PHP hosting server or localhost with proper ftp server name.
- Don’t confused with filename and sub-directory! Many websites assumed sub-directory name also as filename.
Below examples illustrate the ftp_nlist() function in PHP:
Example 1:
PHP
<?php// Connect to FTP server// Use a correct ftp server$ftp_server = "localhost";// Use correct ftp username$ftp_username="username";// Use correct ftp password corresponding// to the ftp username$ftp_userpass="password";// File name or path to upload to ftp server$file = "filetoupload.txt"; // Establishing ftp connection $ftp_connection = ftp_connect($ftp_server) or die("Could not connect to $ftp_server");if($ftp_connection) { echo "successfully connected to the ftp server!"; // Logging in to established connection // with ftp username password $login = ftp_login($ftp_connection, $ftp_username, $ftp_userpass); if($login){ // Checking whether logged in successfully or not echo "<br>logged in successfully!"; // Get file & directory list of current directory $file_list = ftp_nlist($ftp_connection, "."); //output the array stored in $file_list using foreach loop foreach($file_list as $key=>$dat) { echo $key."=>".$dat."<br>"; } } else { echo "<br>login failed!"; } // echo ftp_get_option($ftp_connection, 1); // Closing connection if(ftp_close($ftp_connection)) { echo "<br>Connection closed Successfully!"; } }?> |
Output:
Content of that directory in file manager:
Example 2: Connect to ftp server using port number 21 and then list directory content.
PHP
<?php// Connect to FTP server// Use a correct ftp server$ftp_server = "localhost";// Use correct ftp username$ftp_username="username";// Use correct ftp password corresponding// to the ftp username$ftp_userpass="password";// File name or path to upload to ftp server$file = "filetoupload.txt"; // Establishing ftp connection $ftp_connection = ftp_connect($ftp_server, 21) or die("Could not connect to $ftp_server");if($ftp_connection) { echo "successfully connected to the ftp server!"; // Logging in to established connection // with ftp username password $login = ftp_login($ftp_connection, $ftp_username, $ftp_userpass); if($login){ // Checking whether logged in successfully or not echo "<br>logged in successfully!"; // Get file & directory list of current directory $file_list = ftp_nlist($ftp_connection, "."); //output the array stored in $file_list using foreach loop foreach($file_list as $key=>$dat) { echo $key."=>".$dat."<br>"; } } else { echo "<br>login failed!"; } // echo ftp_get_option($ftp_connection, 1); // Closing connection if(ftp_close($ftp_connection)) { echo "<br>Connection closed Successfully!"; } }?> |
Output:
Content of that directory in file manager:
Reference: https://www.php.net/manual/en/function.ftp-nlist.php




