My FTP Wrapper Class for PHP - The Functions (contd.) (Page 4 of 7 )
The SaveDataToRemoteFile function This function will attempt to create a file called $FileName in the $Directory folder on the FTP server and will write $Data to this file. If the file already exists then an error will be raised:
function SaveDataToRemoteFile($Data, $FileName, $Directory, &$Err) { // Save HTML to a remote file on the FTP server if(!$this->IsStillConnected()) { // Connect is dead, attempt to reconnect $err = ""; @$this->Connect($this->__server, $this->__user, $this->__password, $this->__directory, $err);
if(!$putFile) { $Err = "Couldn't write to $CompleteFileName when trying to save file"; return false; }
// Everything worked OK return true; }
The SwitchDirectory Function This function simply changes into the $Directory folder on the FTP server. If a connection or permission error occurs then $Err will contain the error message:
function SwitchDirectory($Directory, &$Err) { // Switch to another directory on the web server. If we don't // have permissions then an error will occur
if(!$this->IsStillConnected()) { // Connect is dead, attempt to reconnect $err = ""; @$this->Connect($this->__server, $this->__user, $this->__password, $this->__directory, $err);
// Try and change into another directory $chDir = ftp_chdir($this->__conn, $Directory);
if(!$chDir) { $Err = "Couldn't change into directory: $Directory"; return false; } else { // Changing directories worked OK return true; } }
The GetFileListAsArray Function This function will change into the $Directory folder and get a list of files and directories contained in that folder. This function still needs a lot of work, but should work in most cases:
function GetFileListAsArray($Directory, &$Err) { // This function will attempt to change into the specified // directory and retrieve a list of files as an associative // array. This list will include file name, size and date last modified
$err = ""; $fileArray = array();
// Can we switch to the desired directory? if(!$this->SwitchDirectory($Directory, $err)) { $this->HandleError($err); }
// We are in the directory, let's retrieve a list of files $fileList = ftp_rawlist($this->__conn, $Directory);
// Save the list of files if(@is_array($fileList)) { // Interate through the array for($i = 0; $i < sizeof($fileList); $i++) { $itemArray = explode(" ", $fileList[$i]);
// Are we dealing with a file or directory? // If the first letter of the attributes is // "d" then we are dealing with a directory $attributes = $itemArray[0];
// Get the date last modified $fileTimeStamp = $itemArray[sizeof($itemArray)-4] . " " . $itemArray[sizeof($itemArray)-3] . " " . $itemArray[sizeof($itemArray)-2];
sort($fileArray); return $fileArray; } else { $Err = "No files in directory"; return -1; } }
The CreateDirectory Function This function tries to make a new directory called $folderName on the FTP server. If it can create the folder, then the folder is given appropriate rights with the CHMOD command:
function CreateDirectory($folderName, &$Err) { // Makes a new folder on the web server via FTP
if(!$this->IsStillConnected()) { // Connect is dead, attempt to reconnect $err = ""; @$this->Connect($this->__server, $this->__user, $this->__password, $this->__directory, $err);