Results 1 to 4 of 4

Thread: List ftp files/folders

  1. #1

    Thread Starter
    PowerPoster JuggaloBrotha's Avatar
    Join Date
    Sep 2005
    Location
    Lansing, MI; USA
    Posts
    4,286

    List ftp files/folders

    I'm trying to connect to an ftp site and list the files and folders (and subfolders) but all I'm getting is an empty array, it connects fine, but I'm getting nothing from the ftp_nlist() function. When I log into the ftp site with FileZilla there's a whole directory structure available, any ideas to why this doesn't work with php?
    Code:
    <html>
    <head>
      <title>Ftp List</title>
    </head>
    <body>
    <?php
    function ftpRecursiveFileListing($ftpConnection, $path) {
        static $allFiles = array();
        $contents = ftp_nlist($ftpConnection, $path);
    
        if (is_array($contents)) {
            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;
    }
    
    
    // connect and login to FTP server
    $ftp_server = "<removed for forum post>";
    $ftp_userName = "<removed for forum post>";
    $ftp_password = "<removed for forum post>";
    
    $ftp_conn = ftp_connect($ftp_server) or die("Could not connect to $ftp_server");
    $login = ftp_login($ftp_conn, $ftp_userName, $ftp_password);
    
    if ($login) {
        // get file list of current directory
        $file_list = ftpRecursiveFileListing($ftp_conn, ".");
        
        if (!empty($file_list)) {
            foreach($file_list as $file) {
                echo("$file<br />");
            }
        } else {
            echo("No directory list");
        }
        
        // close connection
        ftp_close($ftp_conn);
    } else
        echo("Login failed on <b>$ftp_server</b> for <b>$ftp_userName</b>");
    ?>
    </body>
    </html>
    I'm using xampp for coding/testing this, but when I upload it to the GoDaddy PHP webserver it's not working their either.
    Currently using VS 2015 Enterprise on Win10 Enterprise x64.

    CodeBank: All ThreadsColors ComboBoxFading & Gradient FormMoveItemListBox/MoveItemListViewMultilineListBoxMenuButtonToolStripCheckBoxStart with Windows

  2. #2

    Thread Starter
    PowerPoster JuggaloBrotha's Avatar
    Join Date
    Sep 2005
    Location
    Lansing, MI; USA
    Posts
    4,286

    Re: List ftp files/folders

    After more trial and error with my code I've determined it has to be some PHP environment setting, but saddly I'm not that familiar with PHP to even know where/what settings there are, anyone point me in the right direction?
    Currently using VS 2015 Enterprise on Win10 Enterprise x64.

    CodeBank: All ThreadsColors ComboBoxFading & Gradient FormMoveItemListBox/MoveItemListViewMultilineListBoxMenuButtonToolStripCheckBoxStart with Windows

  3. #3
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: List ftp files/folders

    Except for one point, it seems like it should be right... but I can't quite place my finger on it...
    the one place that doesn't look right to me is this:
    Code:
    if (strpos($currentFile, '.') === false) {
                    ftpRecursiveFileListing($ftpConnection, $currentFile);
                }
    You're not assigning the result from the ftpRecursiveFileListing to anything... but $allFiles is static, and it would be added to it in there, so maybe it's ok.

    Have your tries var_dumping $contents to see if the call of nlist is returning anything? That might be worth looking into.
    Also, you may want to look at ftp_rawlist ... it has a recursive parameter... it returns a lot more than nlist does, but it also handles the recursive folder spelunking for you.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  4. #4

    Thread Starter
    PowerPoster JuggaloBrotha's Avatar
    Join Date
    Sep 2005
    Location
    Lansing, MI; USA
    Posts
    4,286

    Re: List ftp files/folders

    Quote Originally Posted by techgnome View Post
    Except for one point, it seems like it should be right... but I can't quite place my finger on it...
    the one place that doesn't look right to me is this:
    Code:
    if (strpos($currentFile, '.') === false) {
                    ftpRecursiveFileListing($ftpConnection, $currentFile);
                }
    You're not assigning the result from the ftpRecursiveFileListing to anything... but $allFiles is static, and it would be added to it in there, so maybe it's ok.

    Have your tries var_dumping $contents to see if the call of nlist is returning anything? That might be worth looking into.
    Also, you may want to look at ftp_rawlist ... it has a recursive parameter... it returns a lot more than nlist does, but it also handles the recursive folder spelunking for you.

    -tg
    I tried changing it to ftp_rawlist() but it still doesn't give me anything, here's my current code:
    Code:
    <html>
    <head>
      <title>Ftp List</title>
    </head>
    <body>
    <?php
    // connect and login to FTP server
    $ftp_server = "<removed for forum post>";
    $ftp_userName = "<removed for forum post>";
    $ftp_password = "<removed for forum post>";
    $dir = '/';
    
    $ftp_conn = ftp_connect($ftp_server) or die("Could not connect to $ftp_server");
    $login = ftp_login($ftp_conn, $ftp_userName, $ftp_password);
    
    if ($login) {
        // get file list of current directory
        $file_list = ftp_rawlist($ftp_conn, $dir, true);
        
        if (!empty($file_list)) {
            echo "<ul>";
            foreach($file_list as $file) {
                echo("<li>$file</li>");
            }
            echo "</ul>";
        } else {
            echo("No directory list");
        }
        
        // close connection
        ftp_close($ftp_conn);
    } else
        echo("Login failed on <b>$ftp_server</b> for <b>$ftp_userName</b>");
    ?>
    </body>
    </html>
    I'm fairly certain that either PHP no longer does ftp, or I need to change a setting but I haven't a clue what it'd be that I would need to change.

    I'm able to do all this stuff super easily in .Net though, not that knowing that helps anything here.
    Currently using VS 2015 Enterprise on Win10 Enterprise x64.

    CodeBank: All ThreadsColors ComboBoxFading & Gradient FormMoveItemListBox/MoveItemListViewMultilineListBoxMenuButtonToolStripCheckBoxStart with Windows

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width