PHP中ftp的連接與操作


 1.操作類

<?php
class FtpService
{
protected $connect = 0;
public function __construct()
{
$this->connect = $this->openServer();
if(($this->connect === 0) || ($this->connect === 1)) return $this->connect;
}

/**
* 打開並登錄服務器
*
* @return mixed
* 0:服務器連接失敗
* 1:服務器登錄失敗
* resource 連接標識
*/
public function openServer(){
//選擇服務器
$config = config('ftp.'); //ftp配置

//連接服務器
$connect = ftp_connect($config['host'], $config['port']);
if($connect == false) return false;

//登錄服務器
if(!ftp_login($connect, $config['user'], $config['pwd'])){
return false;
}else{
echo "connect success!"; echo "<br>";
}

//打開被動模式,數據的傳送由客戶機啟動,而不是由服務器開始
ftp_pasv($connect, false);

//返回連接標識
return $connect;
}

/**
* 創建目錄並將目錄定位到當請目錄
*
* @param resource $connect 連接標識
* @param string $dirPath 目錄路徑
* @return mixed
* 2:創建目錄失敗
* true:創建目錄成功
*/
public function makeDir($dirPath){
$dirPath = '/' . trim($dirPath, '/');
$dirPath = explode('/', $dirPath);
foreach ($dirPath as $dir){
if($dir == '') $dir = '/';
//判斷目錄是否存在

if(!@ftp_chdir($this->connect, $dir)){
//判斷目錄是否創建成功
if(@ftp_mkDir($this->connect, $dir) == false){
return false;
}
@ftp_chdir($this->connect, $dir);
}
}

//關閉服務器
$this->closeServer($this->connect);
return true;
}

/**
* 關閉服務器
*
* @param resource $connect 連接標識
*/
public function closeServer($connect){
if(!empty($connect)) ftp_close($connect);
}

/**
* 上傳文件
*
* @param string $flag 服務器標識
* @param string $local 上傳文件的本地路徑
* @param string $remote 上傳文件的遠程路徑
* @return int
*/
public function upload($local, $remote){
//上傳文件目錄處理
$mdr = $this->makeDir(dirname($remote));
if($mdr === false) return false;

if (!file_exists($local)){
return false;
}

$connect = $this->openServer();
//上傳文件
$result = ftp_put($connect, $remote, $local, FTP_BINARY);
//關閉服務器
$this->closeServer($connect);

//返回結果
return (!$result) ? false : true;
}

/**
* 刪除文件
*
* @param string $flag 服務器標識
* @param string $remote 文件的遠程路徑
* @return int
*/
public function delete($remote){
//刪除
$result = ftp_delete($this->connect, $remote);
//關閉服務器
$this->closeServer($this->connect);

//返回結果
return (!$result) ? false : true;
}

/**
* 讀取文件
*
* @param string $flag 服務器標識
* @param string $remote 文件的遠程路徑
* @return mixed
*/
public function read($remote){
//讀取
$result = ftp_nlist($this->connect, $remote);

//關閉服務器
$this->closeServer($this->connect);

echo "<pre>";
print_r($result);exit;
//返回結果
foreach ($result as $key => $value){
if(in_array($value, array('.', '..'))) unset($result[$key]);
}
return array_values($result);
}

/**
* 下載文件
*/
public function down($local,$remote){
$result = ftp_get($this->connect,$local,$remote,FTP_BINARY);
$this->closeServer($this->connect);
return $result;
}
}

2.ftp連接配置:
<?php
return [
'host' => 'IP',
'port' => 21,
'user' => '賬號',
'pwd' => '密碼'
];
3.ftp操作調用測試: ftp目錄結構(IP/homes/,下面是在homes目錄下測試)
    public function ftp_test(){
$path = iconv('UTF-8','GB2312',$_SERVER['DOCUMENT_ROOT']."/uploads/photos/test.txt");
$ftp = new FtpService();

// $result = $ftp->delete("homes/test.txt"); //刪除操作
// $result = $ftp->read("homes"); //讀操作
// $result = $ftp->down("test.txt",'/homes/test.txt'); //下載操作
// $result = $ftp->makeDir('/homes/testdir/'); //創建文件夾

$result = $ftp->upload($path,'/homes/test1.txt'); //上傳操作

echo $result;
}
添加ftp擴展:php.ini文件添加:extension=php_ftp.dll




免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM