1. // FTP服务器信息
    2. $ftp_server = "ftp.example.com";
    3. $ftp_username = "your_username";
    4. $ftp_password = "your_password";
    5. $local_file = "path/to/your/local/file.txt"; // 本地文件路径
    6. $remote_file = "/path/to/remote/file.txt"; // 远程文件路径(相对于FTP根目录)
    7. // 连接到FTP服务器
    8. $conn_id = ftp_connect($ftp_server);
    9. if (!$conn_id) {
    10. die("无法连接到 $ftp_server : " . ftp_error());
    11. }
    12. // 登录到FTP服务器
    13. if (@ftp_login($conn_id, $ftp_username, $ftp_password) === false) {
    14. die("无法登录到 $ftp_server : " . ftp_error());
    15. }
    16. // 上传文件到FTP服务器
    17. if (ftp_put($conn_id, $remote_file, $local_file, FTP_BINARY) === false) {
    18. die("无法上传文件: " . ftp_error());
    19. } else {
    20. echo "文件上传成功!";
    21. }
    22. // 关闭FTP连接
    23. ftp_close($conn_id);