if($renameResult == true) { // The file/folder was renamed successfully return true; } else { // Couldn't rename the file/folder if($IsFolder == 0) $Err = "Couldn't rename the selected folder: " . @$php_errormsg; else $Err = "Couldn't rename the selected file: " . @$php_errormsg;
return false; } }
The DeleteFile Function This function attempts to delete a file called $FileName from the FTP server:
function DeleteFile($FileName, &$Err) { // Remove the specified file from 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($deleteResult == true) { // The file/folder was renamed successfully return true; } else { // Couldn't delete the selected file $Err = "Couldn't delete the selected file: " . @$php_errormsg; return false; } }
The DeleteFolder Function This function was one of the hardest to write. It recursively deletes all files and folders from a directory called $FolderName:
function DeleteFolder($FolderName, &$Err) { // Remove the specified folder and all subdirectories/files from the FTP server global $php_errormsg;
if(!$this->IsStillConnected()) { // Connect is dead, attempt to reconnect $err = ""; @$this->Connect($this->__server, $this->__user, $this->__password, $this->__directory, $err);
for ($i = 0; $i < sizeof($content); $i++) { // If we can change into this then it's a directory. // If not, it's a file if($content[$i] != "." && $content[$i] != "..") { if(@ftp_chdir($this->__conn, $content[$i])) { // We have a directory $directories[] = $content[$i]; $dir_counter++; @ftp_cdup($this->__conn); } else { // We have a file $files[] = $content[$i]; $file_counter++; } } }
// Lastly, we change into the directory that we want to delete and see // if we can cdup. If we can, we delete it. @ftp_chdir($this->__conn, $FolderName); @ftp_cdup($this->__conn); @ftp_rmdir($this->__conn, $FolderName);
// Did the recursive folder/file deletion work? return true;
The DoesFileExist Function This function checks if a file called $FileName exists in the directory called $FolderName on the FTP server. It returns true if the file exists, and false if it doesn’t:
function DoesFileExist($FolderName, $FileName, &$Err) { // Does the specified file exist on the remote FTP server? // Returns false on error, true on file exists and 2 if it doesn't exist
global $php_errormsg;
if(!$this->IsStillConnected()) { // Connect is dead, attempt to reconnect $err = ""; @$this->Connect($this->__server, $this->__user, $this->__password, $this->__directory, $err);
if(!@ftp_chdir($this->__conn, $FolderName)) { $Err = $php_errormsg; return false; } else { // We have changed into the directory, let's get a list // of files using ftp_nlist and compare it to see if it exists $fileArray = @ftp_nlist($this->__conn, $FolderName);
if(!is_array($fileArray)) { $Err = $php_errormsg; return false; } else { // Loop through each file and check it if exists for($i = 0; $i < sizeof($fileArray); $i++) { if($fileArray[$i] == "$FolderName/$FileName") { return true; } }
// The file wasn't found, return 2 for not found return 2; } } } }