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($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; }
// Save the HTML to a file which we then upload $fp = @fopen("localfile", "wb");
if(!$fp) { $Err = "Couldn't open a local file for temporary output"; return false; }
// The file was opened OK, let's write to it $filePut = @fputs($fp, $Data, strlen($Data));
if(!$filePut) { $Err = "Couldn't write to a local file for temporary output"; return false; } else { @fclose($fp); }
// Now we can try to write to the remote file $CompleteFileName = $Directory . "/" . $FileName; $LocalFileName = "localfile";
$putFile = @ftp_put($this->__conn, $CompleteFileName, $LocalFileName, FTP_BINARY);
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);
if($err != "") { $this->HandleError($err); die(); } else { return true; } }
// 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];
if(substr($attributes, 0, 1) == "d") $fileType = FT_DIRECTORY; else $fileType = FT_FILE;
// Get the file/directory name $fileName = $itemArray[sizeof($itemArray)-1];
// Get the size of the file $fileSize = $itemArray[sizeof($itemArray)-5];
if(!is_numeric($fileSize)) $fileSize = $itemArray[sizeof($itemArray)-6];
// Get the date last modified $fileTimeStamp = $itemArray[sizeof($itemArray)-4] . " " . $itemArray[sizeof($itemArray)-3] . " " . $itemArray[sizeof($itemArray)-2];
$fileArray[] = array("type" => $fileType, "filename" => $fileName, "filesize" => $fileSize, "filetime" => $fileTimeStamp); }
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);
if($err != "") { $this->HandleError($err); die(); } else { return true; } }
$createResult = @ftp_mkdir($this->__conn, $folderName);
if($createResult == true) { // Can we change the files permissions? $execResult = @ftp_site($this->__conn, 'chmod 0777 ' . $folderName . '/');
if($execResult == true) { return true; } else { $Err = "Couldn't set owner permissions on $folderName"; return false; } } else { $Err = "Couldn't create new folder $folderName"; return false; } }Next: The Functions (contd.) >>
More PHP Articles More By Mitchell Harper |