PHP
  Home arrow PHP arrow Page 4 - 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  
Dedicated Servers  
Moblin 
JMSL Numerical Library 
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 / 45
    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


    (Page 4 of 7 )

    Thanks to PHP and the way that the FTP protocol is wrapped into its ftp_xxx functions, it's also extremely easy to retrieve a file from a remote FTP server using the ftp_get function. Its signature is shown below:

    bool ftp_get ( resource ftp_stream, string local_file, string remote_file, int mode)

    On success, ftp_get returns true. On failure, it returns false. Details of each parameter that it requires are shown below:
    • ftp_stream: The resource generated by a call to ftp_connect.
    • local_file: The name of the file where the remote file will be saved to.
    • remote_file: The name of the remove file which will be copied.
    • mode: The transfer mode that should be used to copy the file from the FTP server to the local file. This should be either one of PHP’s predefined constants: FTP_ASCII or FTP_BINARY.
    Using ftp_get, let's connect to a remote FTP server, retrieve a text file, and then display this text file in the browser:

    if(@ftp_chdir($conn, "/bussys"))
    {
    $gotFile = @ftp_get($conn, "c:/thefile.txt", "readme.txt", FTP_ASCII);

    if($gotFile)
    {
    $fp = fopen("c:/thefile.txt", "rb");

    while($data = fgets($fp, 1024))
    {
    echo $data;
    }

    @fclose($fp);
    }
    else
    {
    echo "ERROR: Couldn't get file";
    }
    }
    else
    {
    echo "ERROR: Couldn't change directories";
    }


    The example above shows just how easy it is to retrieve a file from a remote FTP server. Just as easily as we can download a file, we can also send one to the FTP server. The ftp_put function helps us accomplish this:

    bool ftp_put ( resource ftp_stream, string remote_file, string local_file, int mode)

    The parameters are exactly the same as ftp_get, so let's jump straight into an example. This time, however, I will FTP into my Windows 2000 server machine, which has FTP setup. The exact sample principles apply for any FTP server, so just change the server name and login details when you’re testing the code:

    <?php

    $ftpServer = "s3web";
    $ftpUser = "mitchell";
    $ftpPass = "xxxxx";

    $conn = @ftp_connect($ftpServer);
    $login = @ftp_login($conn, $ftpUser, $ftpPass);

    $putFile = @ftp_put($conn, "/newimage.gif", "c:/mitch.gif", FTP_BINARY);

    if($putFile)
    echo "File uploaded OK.";
    else
    echo "File upload failed.";

    ftp_quit($conn);

    ?>


    And it really is that easy. Before we move on and create our own basic FTP application, I just want to mention the ftp_systype command, which returns a string indicating which type of operating system your FTP server is running. The following code returns "Windows_NT" when I connect to my Windows 2000 FTP server:

    ...

    $conn = @ftp_connect($ftpServer);
    $login = @ftp_login($conn, $ftpUser, $ftpPass);

    echo ftp_systype($conn);

    ...

    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 2 hosted by Hostway