本文共 5622 字,大约阅读时间需要 18 分钟。
# FtpClient 类实现 FTP 协议客户端功能本文将详细介绍一个用于实现 FTP 协议客户端功能的 PHP 类 `FtpClient`。该类能够实现对远程 FTP 服务器的文件上传、下载、删除、目录操作等功能,适用于需要通过 FTP 协议进行文件传输的场景。## 类概述`FtpClient` 类的主要功能包括:1. **连接与登录 FTP 服务器**:通过 `connect` 方法建立 FTP 连接并进行登录。2. **文件上传**:支持本地文件到远程服务器的上传,支持二进制模式和文本模式。3. **文件下载**:实现从远程服务器下载文件到本地的功能。4. **文件删除**:提供删除远程服务器文件的功能。5. **目录操作**:支持创建多级目录、修改目录权限、列目录内容等操作。6. **目录删除**:支持递归删除远程服务器目录及其下的所有文件。7. **文件重命名**:提供重命名文件或目录的功能。## 类详解### 1. 连接与登录类的构造方法 `__construct` 接受一个配置数组,初始化时调用 `initialize` 方法设置服务器相关参数。`connect` 方法负责建立 FTP 连接,并使用 `ftp_login` 方法进行身份验证。```phppublic function __construct(array $config = []){ empty($config) OR $this->initialize($config);} public function initialize(array $config = []){ $this->host = $config['host']; $this->user = $config['user']; $this->pass = $config['pass']; $this->port = isset($config['port']) ? $config['port'] : 21;} public function connect(array $config = []){ empty($config) OR $this->initialize($config); $this->conn = @ftp_connect($this->host, $this->port); if ($this->conn === false) { $this->error = "主机连接失败"; return false; } if (!$this->login()) { $this->error = "服务器登录失败"; return false; } return true;} upload 方法支持本地文件到远程服务器的上传,支持二进制模式和文本模式。默认使用 auto 模式,根据文件扩展名自动选择传输模式。
public function upload($local_file = '', $remote_file = '', $mode = 'auto', $permissions = null){ if (!file_exists($local_file)) { $this->error = "本地文件不存在"; return false; } if ($mode == 'auto') { $ext = $this->getExt($local_file); $mode = $this->setType($ext); } $this->createRemoteDir($remote_file); $mode = ($mode == 'ascii') ? FTP_ASCII : FTP_BINARY; $result = @ftp_put($this->conn, $remote_file, $local_file, $mode); if ($result === false) { $this->error = "文件上传失败"; return false; } return true;} download 方法实现从远程服务器下载文件到本地的功能,支持二进制模式和文本模式,默认使用 auto 模式。
public function download($local_file = '', $remote_file = '', $mode = 'auto'){ if ($mode == 'auto') { $ext = $this->getExt($remote_file); $mode = $this->setType($ext); } $mode = ($mode == 'ascii') ? FTP_ASCII : FTP_BINARY; $result = @ftp_get($this->conn, $local_file, $remote_file, $mode); if ($result === false) { return false; } return true;} delete_file 方法实现删除远程服务器文件的功能。
public function delete_file($remote_file = ''){ $result = @ftp_delete($this->conn, $remote_file); if ($result === false) { return false; } return true;} createRemoteDir 方法用于创建远程服务器目录,支持多级目录创建,并可指定目录权限。
private function createRemoteDir($remote_file = '', $permissions = null){ $remote_dir = dirname($remote_file); $path_arr = explode('/', $remote_dir); $file_name = array_pop($path_arr); $path_count = count($path_arr); foreach ($path_arr as $val) { if (!@ftp_chdir($this->conn, $val)) { $tmp = @ftp_mkdir($this->conn, $val); if ($tmp === false) { echo "目录创建失败,请检查权限及路径是否正确!"; exit; } if ($permissions !== null) { $this->chmod($val, $permissions); } @ftp_chdir($this->conn, $val); } } for ($i = 0; $i < $path_count; $i++) { @ftp_cdup($this->conn); }} deleteDir 方法实现递归删除远程服务器目录及其下的所有文件。
public function deleteDir($remote_dir = ''){ $list = $this->listFile($remote_dir); if (!empty($list)) { $count = count($list); for ($i = 0; $i < $count; $i++) { $file = $list[$i]; if (!preg_match('/\.#/', $file) && @ftp_delete($this->conn, $file)) { $this->deleteDir($file); } else { $this->deleteFile($file); } } } if (@ftp_rmdir($this->conn, $remote_dir) === false) { return false; } return true;} rename 方法实现文件或目录重命名功能。
public function rename($old_file = '', $new_file = ''){ $result = @ftp_rename($this->conn, $old_file, $new_file); if ($result === false) { $this->error = "移动失败"; return false; } return true;} listFile 方法用于列出远程服务器目录内容。
public function listFile($remote_path = ''){ return @ftp_nlist($this->conn, $remote_path);} getExt 方法用于获取文件扩展名。
private function getExt($local_file = ''){ $dot = strrpos($local_file, '.'); return ($dot === false) ? 'txt' : substr($local_file, $dot + 1);} setType 方法根据文件扩展名确定传输模式。
private function setType($ext = ''){ return in_array($ext, ['txt', 'text', 'php', 'phps', 'php4', 'js', 'css', 'htm', 'html', 'phtml', 'shtml', 'log', 'xml'], true) ? 'ascii' : 'binary';} chmod 方法用于修改远程服务器目录权限。
private function chmod($path, $mode = 0755){ return @ftp_chmod($this->conn, $path, $mode);} login 方法负责 FTP 登录。
private function login(){ return @ftp_login($this->conn, $this->user, $this->pass);} 以下是使用 FtpClient 类的示例代码:
// 初始化 FTP 客户端$ftp = new FtpClient([ 'host' => 'example.com', 'user' => 'username', 'pass' => 'password',]);// 连接 FTP 服务器if (!$ftp->connect([ 'host' => 'example.com', 'port' => 21,])) { die("无法连接到 FTP 服务器!");}// 上传文件$upload_result = $ftp->upload('local/file.txt', 'remote/file.txt', 'binary');if ($upload_result === false) { die("文件上传失败!");}// 下载文件$download_result = $ftp->download('remote/file.txt', 'local/file.txt', 'binary');if ($download_result === false) { die("文件下载失败!");}// 删除远程文件$delete_result = $ftp->deleteFile('remote/file.txt');if ($delete_result === false) { die("删除远程文件失败!");}// 关闭 FTP 连接$ftp->close(); 本文介绍了一个功能全面的 FtpClient 类,能够通过 FTP 协议实现文件上传、下载、删除等操作。该类适用于需要在 PHP 项目中对远程服务器进行文件管理的场景。通过合理配置和使用,可以实现对 FTP 服务器的轻松操作。
转载地址:http://ngvfk.baihongyu.com/