Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8860867
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T15:21:37+00:00 2026-06-14T15:21:37+00:00

I am trying to list files from an FTP server. I would like to

  • 0

I am trying to list files from an FTP server. I would like to get an array of sub-directories and files in them as a tree, as shown below:

folder1
       file1.txt
       file2.txt
folder2
       folder2a
               file1.txt
               file2.txt
               file.3txt
       folder2b
              file1.txt

Now my array will be something like

[folder1]=>array(file1.txt,file2.txt) 
[folder2]=>array([folder2a]=>array(file1.txt,file2txt,file3.txt)
[folder2b]=>array(file1.txt))

Note: the array above might not be the exact syntax but just to give an idea of what I am looking for.
I tried ftp_nlist() but seems to only return the files and folders but not the files inside the sub-folders.
Here is a sample on how my code looks like

 // set up basic connection
$conn_id = ftp_connect($ftp_server);

// login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);

// get contents of the ftp directory
$contents = ftp_nlist($conn_id, ".");

// output $contents
var_dump($contents);

With the above it only lists of folders and not files. Anyone with a good idea on how to go around this?
Thank you.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T15:21:38+00:00Added an answer on June 14, 2026 at 3:21 pm

    ftp_nlist() doesn’t fetch files and directories recursively, it just returns all files and folders at the specified path. You could write a function to fetch the results in a recursive manner. Here is an example recursive function that someone wrote, which I found in the PHP ftp_nlist() documentation:

    <?php 
    /** 
     * ftpRecursiveFileListing 
     * 
     * Get a recursive listing of all files in all subfolders given an ftp handle and path 
     * 
     * @param resource $ftpConnection  the ftp connection handle 
     * @param string $path  the folder/directory path 
     * @return array $allFiles the list of files in the format: directory => $filename 
     * @author Niklas Berglund 
     * @author Vijay Mahrra 
     */ 
    function ftpRecursiveFileListing($ftpConnection, $path) { 
        static $allFiles = array(); 
        $contents = ftp_nlist($ftpConnection, $path); 
    
        foreach($contents as $currentFile) { 
            // assuming its a folder if there's no dot in the name 
            if (strpos($currentFile, '.') === false) { 
                ftpRecursiveFileListing($ftpConnection, $currentFile); 
            } 
            $allFiles[$path][] = substr($currentFile, strlen($path) + 1); 
        } 
        return $allFiles; 
    } 
    ?>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to get a list of Files in a Folder from Google
I am trying to get Update queries from a list of files using this
I'm trying to get a directory listing of files on an ftp server using
I trying to get a file from an FTP server using ftp_get from PHP
I'm trying to list all files in a virtual directory and it's sub directories.
i am trying to get a list of files into an array or list
I've trying to get list of updated/added/deleted files from SVN by svnlook using: /usr/bin/svnlook
I am trying to retrieve list of files from a server with the windows
I'm trying to read a list of strings from a file to an array.
I'm trying to use MSBuild to read in a list of files from a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.