PHP
  Home arrow PHP arrow Page 3 - My FTP Wrapper Class for 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

My FTP Wrapper Class for PHP
By: Mitchell Harper
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 20
    2003-02-21

    Table of Contents:
  • My FTP Wrapper Class for PHP
  • Wrapping? Huh?
  • Functions, Functions and More Functions!
  • The Functions (contd.)
  • The Functions (contd.)
  • Examples of Using the MY_FTP Class
  • 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


    My FTP Wrapper Class for PHP - Functions, Functions and More Functions!


    (Page 3 of 7 )

    The FTP wrapper class that I’ve designed took a little over 3 hours to develop, yet has saved me at least 20 hours of coding time over the last 3 months.

    Besides the Connect function (which we looked at above), my FTP wrapper class also support these functions, which we will look at later in detail:
    • MY_FTP: Constructor
    • HandleError: Can be used to handle errors
    • IsStillConnected: Tries to communicate with the FTP server by issuing the ftp_systype function. If this fails, then the connection is dead
    • RetrieveDataFromRemoteFile: Grabs the entire contents of a file from the FTP server and returns it
    • SaveDataToRemoteFile: Creates a file on the FTP server and fills it with data passed to the function
    • SwitchDirectory: Changes into the specified directory
    • GetFileListAsArray: This function is still a work in progress, but will grab a list of files from the remote server, including timestamp and size and return them in an associative array
    • CreateDirectory: Creates a directory on the FTP server
    • RenameFile: Renames a file on the FTP server
    • DeleteFile: Removes a file from the FTP server
    • DeleteFolder: Recursively deletes a folder from the FTP server
    • DoesFileExist: Checks if a file exists on the FTP server
    When working with my FTP wrapper class, some functions may result in errors. I must stress that this class is still in beta testing stages, and will undergo a lot of error checking and rewriting before it is released as part of our commercial product.

    I simply wanted to share it with you so that you could use it in your own PHP scripts and learn more about FTP. Please make sure that you backup all files on your FTP server before you experiment with this class as DevArticles will not be held responsible for any loss or corruption of data that results from using this class. 

    Examining the Functions
    Let’s now take a brief look at each function that composes my FTP wrapper class. After we look at each function I will show you a couple of examples to get you started in your own PHP scripts.

    The Connect function
    This function connects to an FTP server and attempts to change into the directory specified by the fourth parameter, $Directory. The fifth parameter, $Err is a reference variable and will contain the error message if an error occurs:

    function Connect($Server, $User, $Password, $Directory, &$Err)
    {
    // We saw this function earlier so I will not list it here
    }


    The IsStillConnected function
    This function queries the FTP server with the ftp_systype command to check if the connection is still alive. It returns true on success or false on disconnection:

    function IsStillConnected()
    {
    // Attempt to call the ftp_systype to see if the connect
    // to the FTP server is still alive and kicking

    if(!@ftp_systype($this->__conn))
    {
    // The connection is dead
    return false;
    }
    else
    {
    // The connection is still alive
    return true;
    }
    }


    The RetrieveDataFromRemoteFile Function
    This function tries to retrieve the contents of a file from the FTP server. Firstly it changes into the $Directory directory, and then attempts to download the file $FileName. The file is saved locally and its contents are returned to the caller of the function:

    function RetrieveDataFromRemoteFile($FileName, $Directory, &$Err)
    {
    // Change into the remote directory and retrieve the content
    // of a file. Once retrieve, return this value to the caller

    if(!$this->IsStillConnected())
    {
    // Connect is dead, attempt to reconnect
    $err = "";
    @$this->Connect($this->__server, $this->__user, $this->__password, $this->__directory, $err);

    if($err != "")
    {
    $this->HandleError($err);
    die();
    }
    else
    {
    return true;
    }
    }

    // We are now connected, so let's retrieve the file contents.
    // Firstly, we change into the directory
    $chDir = @ftp_chdir($this->__conn, $Directory);

    if(!$chDir)
    {
    $Err = "Couldn't change into directory: $Directory";
    return false;
    }

    // We have changed into the directory, let's attempt to get the file
    $fp = @fopen("localfile", "wb");
    $getFile = @ftp_fget($this->__conn, $fp, $FileName, FTP_BINARY);
    fclose($fp);

    $fp = null;

    if(!$getFile)
    {
    $Err = "Unable to download file: $FileName from $Directory";
    return false;
    }

    // The file was downloaded successfully. Let's open it, read in its
    // contents and return it to the calling function

    $fp = @fopen("localfile", "rb");

    if(!$fp)
    {
    $Err = "Unable to open $FileName after it was downloaded from {$this->__server}";
    return false;
    }

    // Read in the contents of the file to a variable
    $data = "";

    while(!feof($fp))
    {
    $data.= fread($fp, 4096);
    }

    @fclose($fp);

    // Return the HTML from the file
    return $data;
    }

    More PHP Articles
    More By Mitchell Harper


       · Ive been toying with the idea of writing an FTP class too, so Im glad someone else...
     

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