
本文详解如何在php中通过FTP安全、可靠地编辑远程服务器上的文本文件,重点解决ftp_fput()写入空文件、file_exists()误判远程路径、以及无需手动删除旧文件等常见陷阱,并提供可直接复用的健壮实现方案。
本文详解如何在php中通过ftp安全、可靠地编辑远程服务器上的文本文件,重点解决`ftp_fput()`写入空文件、`file_exists()`误判远程路径、以及无需手动删除旧文件等常见陷阱,并提供可直接复用的健壮实现方案。
在Web多站点协同管理场景中,常需通过一个中心化配置文件(如域名白名单 email_domain_whitelist.txt)统一控制权限策略。当各子站需动态更新该文件时,直接操作远程服务器文件成为刚需。然而,开发者常陷入几个典型误区:误将http URL(如 https://example.com/…)传给 file_exists() 或 ftp_delete();混淆 ftp_fput()(需已打开的资源流)与 ftp_put()(直接读取本地文件路径)的适用场景;或过度设计——手动删除再上传,反而引入竞态与失败风险。
以下是一套经过生产验证的解决方案,核心原则是:用 ftp_put() 替代 ftp_fput(),以临时本地文件为中介,利用其自动覆盖特性简化流程。
✅ 正确流程与关键代码
function eri_update_global_whitelist($string, $action = 'add') { // 1. 输入清洗与校验(保持原逻辑) $domains = array_filter( array_map('trim', explode(',', strtolower(str_replace(' ', '', $string)))), function($d) { return strpos($d, '.') !== false; } ); if (empty($domains)) { echo '<br>No valid domains to process.'; return; } // 2. 获取并解析现有白名单 $remote_url = 'https://example.com/eri-webtools-plugin/data/email_domain_whitelist.txt'; $current_content = @file_get_contents($remote_url); $existing_domains = $current_content ? array_filter(array_map('trim', explode(',', strtolower(str_replace(' ', '', $current_content)))),'strlen') : []; // 3. 合并或过滤域名 $new_domains = $action === 'add' ? array_values(array_unique(array_merge($existing_domains, $domains))) : array_values(array_diff($existing_domains, $domains)); // 4. 生成新内容并写入临时文件(关键!) $temp_filename = 'email_domain_whitelist.txt'; $content_to_write = implode(', ', $new_domains); $fp = fopen($temp_filename, 'w'); if (!$fp) { echo '<br>Failed to create temporary file.'; return; } fwrite($fp, $content_to_write); fclose($fp); // 必须关闭,确保内容写入磁盘 // 5. FTP连接与上传(使用 ftp_put,非 ftp_fput) require_once $_SERVER['DOCUMENT_ROOT'] . '/wp-content/plugins/eri-webtools-plugin/ftp_config.php'; $conn = ftp_connect($ftp_server); if (!$conn) { echo '<br>FTP connection failed.'; return; } $login = ftp_login($conn, $ftp_username, $ftp_userpass); if (!$login) { echo '<br>FTP login failed.'; ftp_close($conn); return; } ftp_pasv($conn, true); // 启用被动模式,适配多数防火墙 // ✅ 关键:直接传入临时文件路径,ftp_put 自动读取并覆盖远程文件 $remote_path = '/eri-webtools-plugin/data/email_domain_whitelist.txt'; if (ftp_put($conn, $remote_path, $temp_filename, FTP_ASCII)) { echo '<br>✅ Successfully updated whitelist on remote server.'; } else { echo '<br>❌ FTP upload failed for: ' . $remote_path; } ftp_close($conn); // 6. 清理临时文件(推荐) @unlink($temp_filename); }
⚠️ 注意事项与最佳实践
- 绝对禁止混用协议路径:file_exists()、ftp_delete() 等函数仅支持本地文件系统路径或FTP资源句柄,绝不能传入 https:// 或 http:// URL。若需检查远程文件存在性,应使用 ftp_size()(返回 -1 表示不存在)或先尝试 ftp_get() 到内存。
- ftp_fput() vs ftp_put():
- 临时文件安全:临时文件名建议加入唯一标识(如 uniqid(‘whitelist_’))避免并发冲突;上传成功后务必 unlink() 清理,防止磁盘占用。
- 权限与路径:确保FTP用户对目标目录(如 /eri-webtools-plugin/data/)具有写权限;远程路径应为FTP服务器上的相对路径(从FTP根目录起算),而非网站URL路径。
- 错误处理增强:生产环境应捕获FTP操作返回值,结合 ftp_errno() / ftp_error() 提供更精准的调试信息。
✅ 总结
编辑远程文本文件的本质,是“生成本地内容 → 安全传输 → 原子覆盖”。摒弃对 ftp_fput() 的误用,坚定采用 ftp_put() + 临时文件模式,不仅能规避空文件、路径错误等高频问题,还能借助其内置的覆盖机制,省去冗余的删除步骤,显著提升代码健壮性与可维护性。始终牢记:远程文件操作不是“直接编辑”,而是“重新部署”——以本地为可信源,以FTP为可靠通道,即可优雅达成目标。