php使用ftp遠(yuǎn)程上傳文件類(完美解決主從文件同步問(wè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' ); /** * 打開(kāi)并登錄服務(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; //打開(kāi)被動(dòng)模式,數(shù)據(jù)的傳送由客戶機(jī)啟動(dòng),而不是由服務(wù)器開(kāi)始 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ī)?lái)的php使用ftp遠(yuǎn)程上傳文件類(完美解決主從文件同步問(wèn)題的方法)的全部?jī)?nèi)容了,希望對(duì)大家有所幫助,多多支持腳本之家~
相關(guān)文章
php出現(xiàn)Cannot modify header information問(wèn)題的解決方法大全
我做了一個(gè)統(tǒng)一的出錯(cuò)提示函數(shù),在函數(shù)執(zhí)行里面,先處理出錯(cuò)的地址寫入cookie以方便用戶登陸以后可以直接跳轉(zhuǎn)到要執(zhí)行的這個(gè)頁(yè)面,可是發(fā)現(xiàn)在服務(wù)器上測(cè)試時(shí),竟然提示本地沒(méi)有出現(xiàn)的錯(cuò)誤: Warning: Cannot modify header information - headers already sent by....2008-04-04PHP實(shí)現(xiàn)C#山寨ArrayList的方法
這篇文章主要介紹了PHP實(shí)現(xiàn)C#山寨ArrayList的方法,通過(guò)一個(gè)php自定義類模擬實(shí)現(xiàn)C#中ArrayList的功能,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-07-07PHP使用PDO訪問(wèn)oracle數(shù)據(jù)庫(kù)的步驟詳解
POD擴(kuò)展是在PHP5中加入,該擴(kuò)展提供PHP內(nèi)置類 PDO來(lái)對(duì)數(shù)據(jù)庫(kù)進(jìn)行訪問(wèn),不同數(shù)據(jù)庫(kù)使用相同的方法名,解決數(shù)據(jù)庫(kù)連接不統(tǒng)一的問(wèn)題。下面這篇文章主要給大家介紹了關(guān)于PHP使用PDO訪問(wèn)oracle數(shù)據(jù)庫(kù)的步驟,需要的朋友可以參考下。2017-09-09用mysql_fetch_array()獲取當(dāng)前行數(shù)據(jù)的方法詳解
本篇文章是對(duì)使用mysql_fetch_array()獲取當(dāng)前行數(shù)據(jù)的方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06SESSION信息保存在哪個(gè)文件目錄下以及能夠用來(lái)保存什么類型的數(shù)據(jù)
session默認(rèn)是保存到c:\windows\temp目錄下,但是通過(guò)修改php.ini中的session.save_path值可以改變session的保存路徑2012-06-06php 對(duì)輸入信息的進(jìn)行安全過(guò)濾的函數(shù)代碼
php 對(duì)輸入信息的過(guò)濾代碼,主要是針對(duì)php安全問(wèn)題2012-06-06php字符串函數(shù)學(xué)習(xí)之strstr()
這篇文章主要介紹了php字符串函數(shù)學(xué)習(xí)之strstr(),本文講解了它的定義和用法、參數(shù)描述、提示和注釋以及多個(gè)使用示例,需要的朋友可以參考下2015-03-03