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

    ini_set("display_errors", "1");

    $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...";

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

  • If your code works on dev setup and doesn't work when you move to hosting - that usually has to do with the different hosting OS / Setup. So at this point you will need to start providing your OS details (os version, selinux status, selinux booleans, packages) – Dmitry Zayats Nov 28 '16 at 14:33
  • Also it could be duplicate of this stackoverflow.com/questions/12462898/… – Dmitry Zayats Nov 28 '16 at 14:33
  • @DmitryZayats the script is able to connect to the FTP. It failed only when its trying to download the file from remote server. I am trying to find out the information you have requested. – user_387909 Nov 28 '16 at 15:10
  • OS: CentOS release 6.8 (Final) SELinux status: disabled, No SELinux boolean because it's disabled. Can you be more specific please about which packages you want to know ? – user_387909 Nov 28 '16 at 17:47

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

  • Just tried and it worked. Thanks a lot. You made my day. :) – user_387909 Nov 28 '16 at 20:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.