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);
// 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 = "";