Home >Backend Development >PHP Tutorial >php ftp remote upload file class
/**
* Function: FTP operation class (copy, move, delete files/create directory)
* Remote upload files
* Time: 2011216
* Author: Liu Meng
*
*/
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) @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=false)
{
if($type) $this->dir_mkdirs($newpath);
$thisds->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 the 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-& gt; conn_id, $ PATH, $ newpath);
if (! $ This- & GT; off) "The mobile phone fails, please check whether the authority and the 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 one
*/
function copy_file($path,$newpath,$type=true)
{
$downpath = "c:/tmp.dat";
$this->off = @ftp_get($ This-& gt; conn_id, $ double, $ path, ftp_binary); // download
if (! $ This-& gt; off) "file copy failure, please check whether the authority and the original path are correct! “; ftp_delete($this->conn_id,$path);
If(!$this->off) echo "File deletion failed, please check whether the permissions and path are correct! ";
}
/**
* Method: Delete file
* @path -- path
*/
function dir_mkdirs($path)
{
$path_arr = explode('/',$path); // Get the directory array
$file_name = array_pop($path_arr); // Pop the file name
$path_div = count($path_arr ; $tmp = @ ftp_mkdir($this->conn_id,$val);
exit; $this->conn_id,$val);
up($this-> conn_id);
}
}
/**
* Method: Close FTP connection
*/
function close()
>
//How to use
$ftp = new class_ftp('172.15.0.46',21,'zengzhi','aipu2010');
$ftp->up_file($_FILES['Game_src']['tmp_name'],'/G/'. $_FILES['Game_src']['name']);
$ftp->close();
$src_img='http://172.15.0.46/G/'.$_FILES['Game_src'][' name'];