PHP操作FTP类 (上传下载移动创建等)
封面图
•
2025-11-30
•
PHP
240
0
0
0
在 PHP 中,可以使用内置的 FTP 函数来操作 FTP 服务器,例如上传、下载、移动文件、创建目录等。下面是一个完整的 **PHP 操作 FTP 的类** 示例,包含常见的 FTP 操作功能: --- ## ✅ PHP FTP 操作类(支持上传、下载、删除、重命名、创建目录等) ```php <?php class FtpManager { private $conn_id; private $host; private $user; private $pass; private $port = 21; private $timeout = 30; /** * 构造函数,连接 FTP 服务器 */ public function __construct($host, $user, $pass, $port = 21, $timeout = 30) { $this->host = $host; $this->user = $user; $this->pass = $pass; $this->port = $port; $this->timeout = $timeout; $this->connect(); } /** * 连接 FTP 服务器 */ private function connect() { $this->conn_id = ftp_connect($this->host, $this->port, $this->timeout); if (!$this->conn_id) { throw new Exception("无法连接到 FTP 服务器: " . $this->host); } $login_result = ftp_login($this->conn_id, $this->user, $this->pass); if (!$login_result) { throw new Exception("FTP 登录失败: 用户名或密码错误"); } // 设置被动模式 ftp_pasv($this->conn_id, true); } /** * 上传文件 */ public function upload($local_file, $remote_file) { if (!file_exists($local_file)) { throw new Exception("本地文件不存在: " . $local_file); } $result = ftp_put($this->conn_id, $remote_file, $local_file, FTP_BINARY); if (!$result) { throw new Exception("文件上传失败: " . $local_file); } return true; } /** * 下载文件 */ public function download($remote_file, $local_file) { $result = ftp_get($this->conn_id, $local_file, $remote_file, FTP_BINARY); if (!$result) { throw new Exception("文件下载失败: " . $remote_file); } return true; } /** * 删除文件 */ public function delete($remote_file) { $result = ftp_delete($this->conn_id, $remote_file); if (!$result) { throw new Exception("文件删除失败: " . $remote_file); } return true; } /** * 重命名文件/目录 */ public function rename($old_name, $new_name) { $result = ftp_rename($this->conn_id, $old_name, $new_name); if (!$result) { throw new Exception("文件重命名失败: " . $old_name); } return true; } /** * 创建目录 */ public function createDir($dir_name) { $result = ftp_mkdir($this->conn_id, $dir_name); if (!$result) { throw new Exception("目录创建失败: " . $dir_name); } return true; } /** * 删除目录(递归) */ public function deleteDir($dir_name) { $files = ftp_nlist($this->conn_id, $dir_name); if ($files === false) { throw new Exception("目录不存在或无法读取: " . $dir_name); } foreach ($files as $file) { if ($file == '.' || $file == '..') continue; $full_path = $dir_name . '/' . $file; if (ftp_is_dir($this->conn_id, $full_path)) { $this->deleteDir($full_path); } else { $this->delete($full_path); } } $result = ftp_rmdir($this->conn_id, $dir_name); if (!$result) { throw new Exception("目录删除失败: " . $dir_name); } return true; } /** * 获取远程文件列表 */ public function listFiles($dir = '.') { return ftp_nlist($this->conn_id, $dir); } /** * 关闭连接 */ public function close() { ftp_close($this->conn_id); } } ``` --- ## 📌 使用示例 ```php <?php require 'FtpManager.php'; try { $ftp = new FtpManager('ftp.example.com', 'username', 'password'); // 上传文件 $ftp->upload('local/file.txt', 'remote/file.txt'); // 下载文件 $ftp->download('remote/file.txt', 'local/file_downloaded.txt'); // 删除文件 $ftp->delete('remote/file.txt'); // 重命名文件 $ftp->rename('remote/oldname.txt', 'remote/newname.txt'); // 创建目录 $ftp->createDir('remote/new_folder'); // 删除目录(递归) $ftp->deleteDir('remote/new_folder'); // 列出文件 $files = $ftp->listFiles('.'); print_r($files); $ftp->close(); } catch (Exception $e) { echo '错误:' . $e->getMessage(); } ``` --- ## 🔍 注意事项 - 确保服务器上启用了 `ftp` 扩展。 - 如果使用 FTPS(加密 FTP),需要使用 `ftp_ssl_connect()` 替代 `ftp_connect()`。 - 建议在生产环境中对敏感信息(如用户名、密码)进行加密处理。 --- 如果你需要更高级的功能(如断点续传、带宽限制、日志记录等),也可以基于这个类进一步扩展。 是否需要我提供一个带有错误处理和日志记录的版本?
上一篇:
Linux内核引导
下一篇:
php时间格式转换
标题录入,一次不能超过6条
留言
评论