此 class_ftp 封裝了常見的 FTP 操作,支援連線、上傳、移動、複製、刪除檔案、遞歸建立目錄,並自動處理被動模式。適合需以程式方式管理遠端 FTP 伺服器的專案。
<?php
/**
* FTP 操作類別
* 功能:連線、上傳、移動、複製、刪除檔案、建立目錄
*/
class class_ftp {
public $off; // 操作狀態(成功/失敗)
public $conn_id; // FTP 連線資源
/**
* 建構子:建立 FTP 連線並登入
* @param string $FTP_HOST 主機位址
* @param int $FTP_PORT 連接埠
* @param string $FTP_USER 使用者名稱
* @param string $FTP_PASS 密碼
*/
function __construct($FTP_HOST, $FTP_PORT, $FTP_USER, $FTP_PASS) {
$this->conn_id = @ftp_connect($FTP_HOST, $FTP_PORT) or die("FTP 伺服器連線失敗");
@ftp_login($this->conn_id, $FTP_USER, $FTP_PASS) or die("FTP 伺服器登入失敗");
@ftp_pasv($this->conn_id, true); // 啟用被動模式
}
/**
* 上傳檔案
* @param string $path 本地檔案路徑
* @param string $newpath 遠端儲存路徑(含檔名)
* @param bool $type 若遠端目錄不存在是否自動建立
*/
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 "檔案上傳失敗,請檢查權限及路徑是否正確!";
}
/**
* 移動/重新命名檔案
* @param string $path 原遠端路徑
* @param string $newpath 新遠端路徑
* @param bool $type 若目標目錄不存在是否自動建立
*/
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 "檔案移動失敗,請檢查權限及原路徑是否正確!";
}
/**
* 複製檔案(FTP 無原生複製指令,採下載後再上傳方式實現)
* @param string $path 原遠端路徑
* @param string $newpath 新遠端路徑
* @param bool $type 若目標目錄不存在是否自動建立
*/
function copy_file($path, $newpath, $type = true) {
$downpath = "c:/tmp.dat"; // 暫存本地路徑,建議改為 sys_get_temp_dir()
$this->off = @ftp_get($this->conn_id, $downpath, $path, FTP_BINARY); // 下載
if (!$this->off) {
echo "檔案複製失敗,請檢查權限及原路徑是否正確!";
return;
}
$this->up_file($downpath, $newpath, $type); // 再上傳到新路徑
}
/**
* 刪除檔案
* @param string $path 遠端檔案路徑
*/
function del_file($path) {
$this->off = @ftp_delete($this->conn_id, $path);
if (!$this->off) echo "檔案刪除失敗,請檢查權限及路徑是否正確!";
}
/**
* 遞歸建立遠端目錄(支援多層級路徑)
* @param string $path 遠端目標路徑(含檔名或純目錄)
*/
function dir_mkdirs($path) {
$path_arr = explode('/', $path);
$file_name = array_pop($path_arr); // 移除檔名部分
$path_div = count($path_arr);
foreach ($path_arr as $val) {
if (@ftp_chdir($this->conn_id, $val) === false) {
$tmp = @ftp_mkdir($this->conn_id, $val);
if ($tmp === false) {
echo "目錄建立失敗,請檢查權限及路徑是否正確!";
exit;
}
@ftp_chdir($this->conn_id, $val);
}
}
// 返回根目錄
for ($i = 0; $i < $path_div; $i++) {
@ftp_cdup($this->conn_id);
}
}
/**
* 關閉 FTP 連線
*/
function close() {
@ftp_close($this->conn_id);
}
}
// 測試範例
/**************************************
$ftp = new class_ftp('192.168.100.143', 21, 'user', 'pwd');
//$ftp->up_file('aa.txt', 'a/b/c/cc.txt');
//$ftp->move_file('a/b/c/cc.txt', 'a/cc.txt');
//$ftp->copy_file('a/cc.txt', 'a/b/dd.txt');
//$ftp->del_file('a/b/dd.txt');
$ftp->close();
**************************************/
?>
ftp_pasv(),相容多數防火牆/NAT 環境。dir_mkdirs() 會逐層嘗試切換/建立目錄,確保深層路徑可用。$this->off 回傳布林狀態,並輸出錯誤訊息,實際專案建議改拋出例外或回傳錯誤碼。c:/tmp.dat 改為跨平台的 sys_get_temp_dir() . '/ftp_copy_' . uniqid()。__destruct() 自動關閉連線,或支援 PHP 8.1+ 的 ftp_connection 物件導向 API。這個 FTP 類別提供了完整的基礎操作封裝,開箱即用,適合快速整合至需要遠端檔案管理的 PHP 專案中。