FTP Recurse

Posted on 2005-05-16
Last Modified: 2008-02-26
I need a PHP script or a PHP class that will log into, and retrive a directory listing of all the files in that folder and recurse the directories below it. The data (directory listings) can be placed in a CSV or into MySQL or whatever - as long as I can parse and work with it.
Question by:DrDamnit
    1 Comment
    LVL 11

    Accepted Solution

    I had to remove the max execution time to view all the files on my server; it took a couple minutes. This will print them out tabbed appropriately using lists. You should be able to do whatever you want with the data by modifying the function.
    And you need ftp enabled.

    set_time_limit ( 0 );
    $ftpServer = '';
    $user = 'username';
    $pass = 'password';
    $conn = ftp_connect($ftpServer);
    $login = ftp_login($conn, $user, $pass);

    if ((!$conn) || (!$login))
         die('FTP connection failed!');
    dir_tree('.',$conn);//current directory

    function dir_tree($dir,$conn) {
      echo '<ul>';
      $detail = ftp_rawlist($conn, $dir);
      $list   = ftp_nlist($conn, $dir);
      for($i=0;$i<count($list);$i++) {
         echo '<li>'.$list[$i].'</li>';
         if ($detail[$i]{0} == 'd') dir_tree($list[$i],$conn);
       echo '</ul>';

    Featured Post

    Maximize Your Threat Intelligence Reporting

    Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

    Join & Write a Comment

    Suggested Solutions

    This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
    Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
    The viewer will learn how to count occurrences of each item in an array.
    The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now