PHP FTP Class

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 ');?> * *
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.