PHP FTP Class
Last Update:2016-06-23
Source: Internet
Author: User
Ftp_server = server; $this->ftp_user = Username; $this->ftp_pass = password; $this->ftp_port = port; To establish a connection $this->conn_id = Ftp_connect ($this->ftp_server, $this->ftp_port) or Die ("Cannot connect to $this->ftp_server" ); Attempt to log in if (!ftp_login ($this->conn_id, $this->ftp_user, $this->ftp_pass)) {$this->message ("Connection failed $this- >ftp_user "); } else {$this->message ("Connection succeeded $this->ftp_user");}} Function: Create new directory//$path default to empty directory//Create successful return True, otherwise return false. function Newdir ($path = null) {if ($this->ftp_is_dir ($this->conn_id, $path) | | @ftp_mkdir ($this->conn_id, $path)) return true; if (! $this->newdir (dirname ($path))) return false; Ftp_mkdir ($this->conn_id, $path); return true; }//Verify whether the directory///$path is validated: returns False if the directory returns TRUE. function Ftp_is_dir ($path) {$original _directory = ftp_pwd ($this->conn_id), if (@ftp_chdir ($this->conn_id, $path ) {Ftp_chdir ($this->conn_id, $original _directory); return true;} else return false; }//function: Upload file//$ftppath: FTP server location exists; $localpaTh: Local file location;//Upload successful return true, otherwise return false. function UploadFile ($ftppath = null, $localpath = null) {if (!empty ($ftppath) &&!empty ($localpath)) {$ret = Ftp_ Nb_put ($this->conn_id, $ftppath, $localpath, ftp_binary); while ($ret = = ftp_moredata) {$ret = Ftp_nb_continue ($this->conn_id);} if ($ret! = ftp_finished) {$this->message ("Upload failed"); return false; } else {$this->message ("Upload succeeded"); return true;}} }//function: Delete directory//$dir: To delete the directory//delete successfully returns TRUE, otherwise false. function Deldir ($dir = null) {if (Ftp_rmdir ($this->conn_id, $dir)) {$this->message ("delete directory succeeded"); return true;} else {$this->message ("Failed to delete directory"); return false;} }//function: Return directory//return current directory name function redir () {return ftp_pwd ($this->conn_id);}//function: Delete file//$path: File path//Delete successful return true, otherwise return FA Lse function delfile ($path = null) {if (Ftp_delete ($this->conn_id, $path)) {$this->message ("Delete file succeeded"); return true;} El SE {$this->message ("Delete file failed"); return false;}} Function: Print information//$STR: Information to print function message ($STR = null) {if (!empTy ($str)) {echo $str;}} Function: Close FTP connection function closeftp () {ftp_close ($this->conn_id);}} /* for demonstration; Redir (); $ftppath = '/test '; $locpath = "/home/liye/public_html/php_ftp/test"; $conn->uploadfile ($ftppath, $locpath); $conn->newdir (' test/123/1233 '); $conn->deldir (' test/123/1233 '); $conn->delfile ($ftppath); $conn->delfile (' ftp.php ');?> * *