Home arrow PHP arrow Page 3 - My FTP Wrapper Class for PHP
PHP

My FTP Wrapper Class for PHP


In this article Mitchell shares his FTP wrapper class which he has written in PHP. It includes error checking and many useful functions.

Author Info:
By: Mitchell Harper
Rating: 4 stars4 stars4 stars4 stars4 stars / 33
February 21, 2003
TABLE OF CONTENTS:
  1. · My FTP Wrapper Class for PHP
  2. · Wrapping? Huh?
  3. · Functions, Functions and More Functions!
  4. · The Functions (contd.)
  5. · The Functions (contd.)
  6. · Examples of Using the MY_FTP Class
  7. · Conclusion

print this article
SEARCH DEVARTICLES

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;
}

blog comments powered by Disqus
PHP ARTICLES

- Removing Singletons in PHP
- Singletons in PHP
- Implement Facebook Javascript SDK with PHP
- 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

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials