Home > Backend Development > PHP Tutorial > PHP FTP operation code (upload, copy, move, delete files/create directories)_PHP tutorial

PHP FTP operation code (upload, copy, move, delete files/create directories)_PHP tutorial

WBOY
Release: 2016-07-13 10:30:06
Original
1085 people have browsed it

Copy code The code is as follows:

/**
* Function: FTP operations (copy, move, delete files/create directories)
* Time: 2006/5/9
* Author: Xinran Suifeng
* QQ: 276624915
*/
class class_ftp
{
public $off; // Return operation status (success/failure)
public $conn_id; // FTP connection
/**
* Method: FTP connection
* @FTP_HOST -- FTP host
* @FTP_PORT -- Port
* @FTP_USER -- Username
* @FTP_PASS -- Password
*/
function __construct($FTP_HOST,$FTP_PORT,$FTP_USER,$FTP_PASS)
{
$this->conn_id = @ftp_connect($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); // Turn on passive simulation
}
/**
* Method: Upload file
* @path -- local path
* @newpath -- upload path
* @type -- if the target directory does not exist, create a new one
*/
function 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 whether the permissions and path are correct!";
}
/**
* Method: Move file
* @path -- Original path
* @newpath -- New path
* @type -- If the target directory does not exist, create a new one
*/
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 whether the permissions and original path are correct!";
}
/**
* Method: Copy the file
* Note: Since FTP does not have a copy command, the alternative operation of this method is: download and then upload to a new path
* @path -- original path
* @newpath -- New path
* @type -- If the target directory does not exist, create a new path
*/
function copy_file($path,$newpath,$type=true)
{
$downpath = "c:/tmp.dat";
$ This-& GT; OFF = @FTP_get ($ this-& gt; conn_id, $ double, $ PATH, ftp_binary); // Download
if (! $ This-& GT; OFF) " Check whether the permissions and original path are correct! ",
               $this->off = @ftp_delete($this->conn_id,$path); !";
}
/**
* Method: Generate directory
* @path -- path
*/
function dir_mkdirs($path)
{
$path_arr = explode('/',$path); // Get directory Array
$file_name = array_pop($path_arr); // Pop file name
$path_div = count($path_arr); // Get the number of layers
foreach($path_arr as $val) // Create directory
                                                                                                                                                                                                                                                                                                _id,$val);
IF ($ TMP == FALSE)
{
Echo "directory failed, please check whether the authority and path are correct! ";
exit;
}
@ftp_chdir($this->conn_id,$val);
}
}
for($i=1;$i=$ path_div;$i++) // Fall back to the root
                                                                                                                                                                                                             > function close()
{
@ftp_close($this->conn_id);
}
}// class class_ftp end
/**
* Method: Close FTP connection
*/
?>






http://www.bkjia.com/PHPjc/768126.html

www.bkjia.com

true

http: //www.bkjia.com/PHPjc/768126.html

TechArticle
Copy the code code as follows: ?php /************************************** test************* ************************$ftp = new class_ftp('192.168.100.143',21,'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 file

//$ftp->copy_file('a/cc.txt','a/b/ dd.txt'); // Copy file$ftp->close(); / / Close FTP connection ************************************************ **********************************/ class cla...
Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template