My FTP Wrapper Class for PHP - The Functions (contd.) (Page 5 of 7 ) The RenameFile Function This function attempts to rename a file on the FTP server from $OldFileName to $NewFileName: function RenameFile($OldFileName, $NewFileName, $IsFolder, &$Err) { // Rename a file/directory on 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);
if($err != "") { $this->HandleError($err); die(); } else { return true; } }
$renameResult = @ftp_rename($this->__conn, $OldFileName, $NewFileName);
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($err != "") { $this->HandleError($err); die(); } else { return true; } }
$deleteResult = @ftp_delete($this->__conn, $FileName);
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);
if($err != "") { $this->HandleError($err); die(); } else { return true; } }
@ftp_chdir($this->__conn, $FolderName); $location = @ftp_pwd($this->__conn);
$directories = array(); $files = array(); $dir_counter = 0; $file_counter = 0; $content = @ftp_nlist($this->__conn, ".");
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++; } } }
for ($j = 0; $j < $file_counter; $j++) { @ftp_delete($this->__conn, $files[$j]); }
for ($j = 0; $j < $dir_counter; $j++) { if($directories[$j] != "." OR $directories[$j] != "..") { $location = ftp_pwd ($this->__conn); $this->DeleteFolder($directories[$j], $this->__dummyError); @ftp_cdup ($this->__conn); @ftp_rmdir($this->__conn,$directories[$j]); } }
// 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;
/* if(@$php_errormsg == "") { return true; } else { $Err = "Couldn't recursive delete folder: " . @$php_errormsg; return false; } */ } 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($err != "") { $this->HandleError($err); die(); } else { return true; } }
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; } } } }Next: Examples of Using the MY_FTP Class >>
More PHP Articles More By Mitchell Harper |