我正在尝试使用PHP从FTP下载文件。 我已经testing了2台服务器的脚本,它工作正常。 但是在我需要运行这个脚本的服务器上却不起作用。 任何帮助将是可观的。
我得到这个错误。
警告:ftp_nb_fget():在第18行的/home/sites/example.com/public_html/path-to-file/download-file.php中将types设置为I.
<?php ini_set("display_errors", "1"); error_reporting(E_ALL); $ftp_server = "server_address"; $ftp_username = "username"; $ftp_userpass = "password"; $ftp_conn = ftp_connect($ftp_server) or die("Could not connect to $ftp_server"); $login = ftp_login($ftp_conn, $ftp_username, $ftp_userpass); $src_file = 'source_file'; //File to write $dest_file = 'server_file'; //File to download $data_file = fopen($src_file, 'w'); // Initate the download $ret = ftp_nb_fget($ftp_conn, $data_file, $dest_file, FTP_BINARY); while ($ret == FTP_MOREDATA) { // Do whatever you want echo "."; // Continue downloading... $ret = ftp_nb_continue($ftp_conn); } if ($ret != FTP_FINISHED) { echo "There was an error downloading the file..."; exit(1); } ?>
我也尝试了ftp_get而不是ftp_nb_fget,但是得到和上面一样的错误。
基本上可能发生什么 – 你在防火墙后面,但试图使用主动FTP会话(你是)。
这将解释为什么你的FTP会话正确build立,但试图获取文件失败。
在这里看看如何使用被动ftp的方式