Php – Error downloading file from FTP using PHP

ftpPHP

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.

Best Answer

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