1

I have a windows server from which i wish to transfer some images to my FTP server. below is my code

    $file = "docs/1.png";
    $ftp_server = "";
    $ftp_user_name = "";
    $ftp_user_pass = '';
    $destination_file = "1.png";

    // set up basic connection
    $conn_id = ftp_connect($ftp_server);


    // login with username and password
    $login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass); 

    // check connection
    if ((!$conn_id) || (!$login_result)) { 
        echo "FTP connection has failed!";
        echo "Attempted to connect to $ftp_server for user $ftp_user_name"; 
        exit; 
    } else {
        echo "Connected to $ftp_server, for user $ftp_user_name";
    }
    echo "Current directory is now: " . ftp_pwd($conn_id) . "\n";
    // upload the file
    // ftp_set_option($conn_id, FTP_TIMEOUT_SEC, 180);
    ftp_chdir($conn_id, "example/");
    echo "Current directory is now: " . ftp_pwd($conn_id) . "\n";
    // ftp_put($conn_id, $destination_file, $file, FTP_BINARY); 
    ftp_pasv($conn_id, true); 
    $upload = ftp_put($conn_id, $destination_file, $file, FTP_BINARY); 

    // check upload statu
    if (!$upload) { 
    echo "FTP upload has failed!";
    } else {
    echo "Uploaded $source_file to $ftp_server as $destination_file";
    }

    // close the FTP stream 
    ftp_close($conn_id);

I am getting **

ftp_put(): Opening BINARY mode data connection

** this error and file not uploading.

  • Sounds like the typical firewall issue you always run into when using the 1970th FTP protocol. Google for "ftp firewall ports" or think yourself what port range you need to open for that or learn about passive mode. Or, _much better_ stop using the totally outdated FTP protocol and switch to something more secure and easier to setup, most likely `sftp`. – arkascha Mar 02 '18 at 10:36
  • Have you looked at [this](https://stackoverflow.com/questions/2496472/php-ftp-get-warning-ftp-get-opening-binary-mode-data-connection) ? – ka_lin Mar 02 '18 at 10:43
  • thanks i will arkascha and ka_lin – Manikandan Venkatesan Mar 02 '18 at 10:46

0 Answers0