PHP
  Home arrow PHP arrow Page 3 - Building An FTP Client With PHP
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PHP

Building An FTP Client With PHP
By: Mitchell Harper
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 46
    2002-07-02

    Table of Contents:
  • Building An FTP Client With PHP
  • PHP and FTP 101
  • Retrieving a File Listing
  • Retrieving a File
  • Building an FTP App
  • The GetFile function
  • Conclusion

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Building An FTP Client With PHP - Retrieving a File Listing


    (Page 3 of 7 )

    The ftp_nlist function returns a list of files in a given directory. We can use this function in combination with ftp_pwd to retrieve a list of files and directories from the Microsoft FTP web site. Its signature looks like this:

    array ftp_nlist ( resource ftp_stream, string directory)

    We can return a list of files and directories with the following extract of code:

    <?php

    $ftpServer = "ftp.microsoft.com";
    $ftpUser = "anonymous";
    $ftpPass = "me@myhost.com";

    set_time_limit(160);

    $conn = @ftp_connect($ftpServer)
    or die("Couldn't connect to FTP server");

    $login = @ftp_login($conn, $ftpUser, $ftpPass)
    or die("Login credentials were rejected");

    $workingDir = ftp_pwd($conn);

    echo "Files for directory: $workingDir<br><br>";

    $fList = @ftp_nlist($conn, $workingDir);

    if(is_array($fList))
    {
    for($i = 0; $i < sizeof($fList); $i++)
    {
    echo $fList[$i] . "<br>";
    }
    }
    else
    {
    echo "$workingDir contains no files.";
    }


    ftp_quit($conn);

    ?>


    The output from the code above looks like this:

    Files for directory: /

    /bussys
    /deskapps
    /developr
    /kbhelp
    /misc
    /misc1
    /peropsys
    /products
    /reskit
    /services
    /softlib

    All of the values returned from ftp_nlist were directories in this case. One of the great features of the ftp_nlist function is that we can use a wildcard value for its second parameter, like this:

    $fList = ftp_nlist($conn, "/p*");

    This would return all files and directories in the root directory that started with the letter "p". It's great to list the files in a directory, but we also need to be able to change into other directories to traverse the remote FTP server. We can do this in PHP by using the ftp_chdir function, whose signature looks like this:

    bool ftp_chdir ( resource ftp_stream, string directory)

    The first parameter is the connection resource and the second is the name of the directory that we wish to change into. We can change into the first available directory and list its files with this snippet of code:

    $workingDir = ftp_pwd($conn);
    $fList = ftp_nlist($conn, $workingDir);

    if(is_array($fList))
    {
    $newDir = $fList[0];

    if(@ftp_chdir($conn, $newDir))
    {
    $fList1 = ftp_rawlist($conn, $newDir);

    if(is_array($fList1))
    {
    echo "Files for directory: $newDir<br><br>";

    for($i = 0; $i < sizeof($fList1); $i++)
    {
    echo $fList1[$i] . "<br>";
    }
    }
    else
    {
    echo "$newDir contains no files.";
    }
    }
    else
    {
    echo "An error occured while changing to $newDir or $newDir is not a directory";
    }
    }

    else
    {
    echo "$workingDir contains no files.";
    }


    If the code shown above looks a little complicated, allow me to explain it. First off, we use the ftp_nlist command to retrieve a list of directories and files from the FTP server. We then attempt to change into that directory with ftp_chdir. If this fails, then we've either tried to treat a file as a directory, or an error occurred on the server.

    Next, we use the ftp_rawlist function to retrieve the files and directories from our new directory and list then using a for loop, exactly the same way we have in previous examples. The only difference between ftp_rawlist and ftp_nlist is that ftp_rawlist also contains file permissions, owners, and the date that each file or directory was last modified.

    The output from the above script looked like this on my PC:

    Files for directory: /bussys

    dr-xr-xr-x 1 owner group 0 Nov 30 2000 1394
    dr-xr-xr-x 1 owner group 0 Jun 6 2000 backoffice
    dr-xr-xr-x 1 owner group 0 Mar 1 2000 clients
    dr-xr-xr-x 1 owner group 0 Jan 26 2001 distapps
    dr-xr-xr-x 1 owner group 0 Feb 24 2000 exchange
    dr-xr-xr-x 1 owner group 0 Jul 2 12:14 gen_info
    dr-xr-xr-x 1 owner group 0 Feb 24 2000 iis
    dr-xr-xr-x 1 owner group 0 Jul 2 12:14 lanman
    dr-xr-xr-x 1 owner group 0 Jul 2 12:13 mail
    dr-xr-xr-x 1 owner group 0 Feb 24 2000 mcis
    dr-xr-xr-x 1 owner group 0 Oct 5 2000 mspress
    -r-xr-xr-x 1 owner group 1715 May 20 1996 readme.txt
    -r-xr-xr-x 1 owner group 2107 Jul 2 11:25 readme1.txt
    dr-xr-xr-x 1 owner group 0 Feb 24 2000 sitesrv
    dr-xr-xr-x 1 owner group 0 Jul 2 12:46 sql
    dr-xr-xr-x 1 owner group 0 Mar 14 2000 sysmgrsrv
    dr-xr-xr-x 1 owner group 0 Feb 24 2000 utilities
    dr-xr-xr-x 1 owner group 0 Feb 24 2000 viper
    dr-xr-xr-x 1 owner group 0 Jul 2 12:45 winnt
    dr-xr-xr-x 1 owner group 0 Feb 25 2000 winsock

    More PHP Articles
    More By Mitchell Harper


     

    PHP ARTICLES

    - Making Usage Statistics in PHP
    - Installing PHP under Windows: Further Config...
    - File Version Management in PHP
    - Statistical View of Data in a Clustered Bar ...
    - Creating a Multi-File Upload Script in PHP
    - Executing Microsoft SQL Server Stored Proced...
    - Code 10x More Efficiently Using Data Access ...
    - A Few Tips for Speeding Up PHP Code
    - The Modular Web Page
    - Quick E-Commerce with PHP and PayPal
    - Regression Testing With JMeter
    - Building an Iterator with PHP
    - PHP Frontend to ImageMagick
    - Using PEAR's mimeDecode Module
    - Incoming Mail and PHP






    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway
    Stay green...Green IT