大网数据广告活动
服务器运维问答

Error downloading file from FTP using PHP

· 52CE 我爱测

I am trying to download a file from FTP using PHP. I have tested the script in 2 server and it works fine. But it doesn't work in the server where I need this script to be run. Any help would be appreciable. I am getting this err…

问题原文

原帖链接 · user_387909 · CC BY-SA 3.0

I am trying to download a file from FTP using PHP. I have tested the script in 2 server and it works fine. But it doesn't work in the server where I need this script to be run. Any help would be appreciable.

I am getting this error.

Warning: ftp_nb_fget(): Type set to I. in /home/sites/example.com/public_html/path-to-file/download-file.php on line 18

<?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);
    }
    ?>

I have also tried ftp_get instead of ftp_nb_fget but getting same error as above.

已采纳解答

原帖链接 · Dmitry Zayats · CC BY-SA 3.0

Basically what could be happening - you are behind the firewall but trying to use active ftp session (which you are).
That would explain why your ftp session is established correctly, but trying to fetch file fails.

Have a look here on the way how to use passive ftp

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2016-11-28)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。