php使用ftp遠(yuǎn)程上傳文件類(完美解決主從文件同步問題的方法)
php使用ftp實(shí)現(xiàn)文件上傳代碼片段:
<?php
/**
* ftp上傳文件類
*/
class Ftp {
/**
* 測(cè)試服務(wù)器
*
* @var array
*/
private $testServer = array(
'host' => 'ip',
'port' => 21,
'user' => 'userName',
'pwd' => 'password'
);
/**
* 打開并登錄服務(wù)器
*
* @param string $flag 服務(wù)器標(biāo)識(shí)test
* @return mixed
* 0:服務(wù)器連接失敗
* 1:服務(wù)器登錄失敗
* resource 連接標(biāo)識(shí)
*/
public function openServer($flag = 'test'){
//選擇服務(wù)器
$config = $this->getServerConfig($flag);
//連接服務(wù)器
$connect = ftp_connect($config['host'], $config['port']);
if($connect == false) return 0;
//登錄服務(wù)器
if(!ftp_login($connect, $config['user'], $config['pwd'])) return 1;
//打開被動(dòng)模式,數(shù)據(jù)的傳送由客戶機(jī)啟動(dòng),而不是由服務(wù)器開始
ftp_pasv($connect, true);
//返回連接標(biāo)識(shí)
return $connect;
}
/**
* 創(chuàng)建目錄并將目錄定位到當(dāng)請(qǐng)目錄
*
* @param resource $connect 連接標(biāo)識(shí)
* @param string $dirPath 目錄路徑
* @return mixed
* 2:創(chuàng)建目錄失敗
* true:創(chuàng)建目錄成功
*/
public function makeDir($connect, $dirPath){
//處理目錄
$dirPath = '/' . trim($dirPath, '/');
$dirPath = explode('/', $dirPath);
foreach ($dirPath as $dir){
if($dir == '') $dir = '/';
//判斷目錄是否存在
if(@ftp_chdir($connect, $dir) == false){
//判斷目錄是否創(chuàng)建成功
if(@ftp_mkDir($connect, $dir) == false){
return 2;
}
@ftp_chdir($connect, $dir);
}
}
return true;
}
/**
* 關(guān)閉服務(wù)器
*
* @param resource $connect 連接標(biāo)識(shí)
*/
public function closeServer($connect){
if(!empty($connect)) ftp_close($connect);
}
/**
* 上傳文件
*
* @param string $flag 服務(wù)器標(biāo)識(shí)
* @param string $local 上傳文件的本地路徑
* @param string $remote 上傳文件的遠(yuǎn)程路徑
* @return int
* 0:服務(wù)器連接失敗
* 1:服務(wù)器登錄失敗
* 2:創(chuàng)建目錄失敗
* 3:上傳文件失敗
* 4:上傳成功
*/
public function upload($flag = 'test', $local, $remote){
//連接并登錄服務(wù)器
$connect = $this->openServer($flag);
if(($connect === 0) || ($connect === 1)) return $connect;
//上傳文件目錄處理
$mdr = $this->makeDir($connect, dirname($remote));
if($mdr === 2) return 2;
//上傳文件
$result = ftp_put($connect, basename($remote), $local, FTP_BINARY);
//關(guān)閉服務(wù)器
$this->closeServer($connect);
//返回結(jié)果
return (!$result) ? 3 : 4;
}
/**
* 刪除文件
*
* @param string $flag 服務(wù)器標(biāo)識(shí)
* @param string $remote 文件的遠(yuǎn)程路徑
* @return int
* 0:服務(wù)器連接失敗
* 1:服務(wù)器登錄失敗
* 2:刪除失敗
* 3:刪除成功
*/
public function delete($flag = 'test', $remote){
//連接并登錄服務(wù)器
$connect = $this->openServer($flag);
if(($connect === 0) || ($connect === 1)) return $connect;
//刪除
$result = ftp_delete($connect, $remote);
//關(guān)閉服務(wù)器
$this->closeServer($connect);
//返回結(jié)果
return (!$result) ? 2 : 3;
}
/**
* 讀取文件
*
* @param string $flag 服務(wù)器標(biāo)識(shí)
* @param string $remote 文件的遠(yuǎn)程路徑
* @return mixed
* 0:服務(wù)器連接失敗
* 1:服務(wù)器登錄失敗
*/
public function read($flag, $remote){
//連接并登錄服務(wù)器
$connect = $this->openServer($flag);
if(($connect === 0) || ($connect === 1)) return $connect;
//讀取
$result = ftp_nlist($connect, $remote);
//關(guān)閉服務(wù)器
$this->closeServer($connect);
//返回結(jié)果
foreach ($result as $key => $value){
if(in_array($value, array('.', '..'))) unset($result[$key]);
}
return array_values($result);
}
/**
* 獲取ftp服務(wù)器配置
*
* @param string $flag 服務(wù)器標(biāo)識(shí)test
* @return array ftp服務(wù)器連接配置
*/
private function getServerConfig($flag = 'test'){
$flag = strtolower($flag);
//測(cè)試服務(wù)器
if($flag == 'test') return $this->testServer;
//默認(rèn)返回測(cè)試服務(wù)器
return $this->testServer;
}
}
?>
以上就是小編為大家?guī)淼膒hp使用ftp遠(yuǎn)程上傳文件類(完美解決主從文件同步問題的方法)的全部?jī)?nèi)容了,希望對(duì)大家有所幫助,多多支持腳本之家~
相關(guān)文章
探討Smarty中如何獲取數(shù)組的長(zhǎng)度以及smarty調(diào)用php函數(shù)的詳解
本篇文章是對(duì)Smarty中如何獲取數(shù)組的長(zhǎng)度以及smarty調(diào)用php函數(shù)的方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06
PHP實(shí)現(xiàn)將Word文件保存到SQL Server數(shù)據(jù)庫
這篇文章主要介紹了如何利用PHP實(shí)現(xiàn)將上傳的Word文件保存到SQL Server數(shù)據(jù)庫,文中的示例代碼講解詳細(xì),需要的可以參考一下2022-02-02
詳解PHP結(jié)構(gòu)型設(shè)計(jì)模式之橋接模式Bridge Pattern
橋接,顧名思義,就是用來連接兩個(gè)部分,使得兩個(gè)部分可以互相通訊。橋接模式將系統(tǒng)的抽象部分與實(shí)現(xiàn)部分分離解耦,使他們可以獨(dú)立的變化。本文通過示例詳細(xì)介紹了橋接模式的原理與使用,需要的可以參考一下2023-04-04
PHP實(shí)現(xiàn)的自定義數(shù)組排序函數(shù)與排序類示例
這篇文章主要介紹了PHP實(shí)現(xiàn)的自定義數(shù)組排序函數(shù)與排序類,結(jié)合實(shí)例形式分析了php自定義二維數(shù)組排序函數(shù)與排序類的相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2016-11-11
PHP實(shí)現(xiàn)的消息實(shí)時(shí)推送功能【基于反ajax推送】
這篇文章主要介紹了PHP實(shí)現(xiàn)的消息實(shí)時(shí)推送功能,結(jié)合實(shí)例形式分析了php基于反ajax推送實(shí)現(xiàn)的消息實(shí)時(shí)推送前臺(tái)ajax提交、后臺(tái)數(shù)據(jù)處理等相關(guān)操作技巧,需要的朋友可以參考下2018-03-03
PHP 正則判斷中文UTF-8或GBK的思路及具體實(shí)現(xiàn)
UTF-8匹配: 在javascript中,要判斷字符串是中文是很簡(jiǎn)單的,下面有個(gè)不錯(cuò)的判斷示例,需要的朋友可以參考下2013-11-11
深入分析使用mysql_fetch_object()以對(duì)象的形式返回查詢結(jié)果
本篇文章是對(duì)使用mysql_fetch_object()以對(duì)象的形式返回查詢結(jié)果進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06
php中有關(guān)合并某一字段鍵值相同的數(shù)組合并的改進(jìn)
這篇文章主要介紹了php中有關(guān)合并某一字段鍵值相同的數(shù)組合并的改進(jìn),需要的朋友可以參考下2015-03-03

