PHP
  Home arrow PHP arrow Page 6 - 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 - The GetFile function


    (Page 6 of 7 )

    GetFile makes use of the ftp_get command to retrieve one selected file from the remote FTP server. After the call to ftp_get, a status message is displayed at the top of the screen indicating whether or not the file was downloaded successfully: a yellow message is shown on success, and a red one on failure.

    GetFile starts out by working out where the file should be saved to:

    function GetFile()
    {
    $conn = DoConn();
    $currDir = "";

    // Get the name of the current directory
    if(isset($_GET["currFile"]))
    {
    $currFile = $_GET["currFile"];

    // We need to work out the name of the file
    // by stripping away the path, etc

    $localFile = strrev($currFile);
    $localFile = substr($localFile, 0, strpos($localFile, "/"));
    $localFile = strrev($localFile);
    $localFile = "___$localFile";


    As mentioned earlier, the name of the file is prefixed with three underscores and saved to the root directory on your local machine. The reason I've prefixed the files with three underscores is so that you don't overwrite any important system files by accidentally download files with the same name from the remote server.

    GetFile is basically just one call to ftp_get with some error handling:

    if(@ftp_get($conn, $localFile, $currFile, FTP_BINARY))
    {
    // Success
    }
    else
    {
    // Failure
    }


    If the file was retrieved OK, then a message like the following is shown at the top of the screen:

    A successful file retrieval

    On the other hand, if something goes wrong then a message like the one shown below is displayed:

    A failed file retrieval

    Now, at this point we've taken a look at both the ShowFiles and GetFile functions. If you've never worked with PHP and FTP before, then let me set this challenge for you:

    I'm not going to provide you with the code for the PutFile, RenameFile or DeleteFile functions. Instead, head on over to the PHP FTP reference page and see if you can workout how to accomplish these functions using PHP's built-in FTP functionality. It's actually quite easy to do, and by doing so you will help yourself to become a better coder.

    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 5 hosted by Hostway
    Stay green...Green IT