FTP Operation class---copying, moving, deleting files/creating directories

Source: Internet
Author: User
Tags create directory ftp connection

FTP Operation class---copying, moving, deleting files/creating directories

<?php/*** Function: FTP operation class (copy, move, delete file/create directory) * Time: 2006/5/9* gladly with the wind * qq:276624915*/class class_ftp{public $off;//return operation status (successful /failure) public $conn _id;  FTP Connection/** * Method: FTP Connection * @FTP_HOST--FTP host * @FTP_PORT--Port * @FTP_USER--User name * @FTP_PASS --Password */function __construct ($FTP _host, $FTP _port, $FTP _user, $FTP _pass) {$this->conn_id = @ftp_conn         ECT ($FTP _host, $FTP _port) or Die ("FTP Server Connection Failed");         @ftp_login ($this->conn_id, $FTP _user, $FTP _pass) or Die ("FTP server login failed"); @ftp_pasv ($this->conn_id,1); Open Passive Impersonation}/** * Method: Upload file * @path--Local Path * @newpath--Upload path * @type--If the target directory does not exist the new */F        Unction Up_file ($path, $newpath, $type =true) {if ($type) $this->dir_mkdirs ($newpath);         $this->off = @ftp_put ($this->conn_id, $newpath, $path, ftp_binary); if (! $this->off) echo "File upload failed, please check the permissions and path is correct!"     "; }/** * Method: Move File * @path--The original path * @newpath--New path * @type--If the target directory does not exist, new */function Move_file ($path, $newpath, $type =true) {if ($type) $this->dir_mkdirs ($newpath        );         $this->off = @ftp_rename ($this->conn_id, $path, $newpath); if (! $this->off) echo "File move failed, please check the permissions and the original path is correct!"     "; }/** * Method: Copy File * Description: Due to FTP no copy command, this workaround is: Download and then upload to the new path * @path-the original path * @newpath-New path * @type        -If the target directory does not exist, the new */function Copy_file ($path, $newpath, $type =true) {$downpath = "c:/tmp.dat"; $this->off = @ftp_get ($this->conn_id, $downpath, $path, ftp_binary);//download if (! $this->off) echo "File copy failed, please check permissions and The original path is correct!        ";     $this->up_file ($downpath, $newpath, $type); }/** * Method: Delete file * @path--Path */function Del_file ($path) {$this->off = @ftp_delete ($t         his->conn_id, $path); if (! $this->off) echo "File deletion failed, please check the permissions and path is correct!"     "; }/** * Method: Generate directory * @path-Path */function Dir_mkdirs ($path) {$path _arr = expLode ('/', $path); Fetch directory array $file _name = Array_pop ($path _arr); Pop-up file name $path _div = count ($path _arr);             Number of layers foreach ($path _arr as $val)//Create directory {if (@ftp_chdir ($this->conn_id, $val) = = FALSE)                 {$tmp = @ftp_mkdir ($this->conn_id, $val); if ($tmp = = FALSE) {echo ' Directory creation failed, check the permissions and path are correct!                     ";                 Exit             } @ftp_chdir ($this->conn_id, $val);         }} for ($i =1; $i <= $path _div; $i + +)//fallback to root {@ftp_cdup ($this->conn_id);     }}/** * method: Close FTP connection */function Close () {@ftp_close ($this->conn_id); }}//class class_ftp end/************************************** test *********************************** $ftp = new Class _ftp (' 192.168.100.143 ', +, ' user ', ' pwd '); Open FTP connection//$ftp->up_file (' aa.txt ', ' a/b/c/cc.txt '); Upload file//$ftp->move_file (' A/b/c/cc.txt ', 'A/cc.txt '); Move files//$ftp->copy_file (' a/cc.txt ', ' a/b/dd.txt '); Copy file//$ftp->del_file (' a/b/dd.txt '); Delete file $ftp->close (); Close the FTP connection ******************************************************************************/?>

Detailed Description: http://php.662p.com/thread-548-1-1.html

FTP Operation class---copying, moving, deleting files/creating directories

Related Article

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.