FTP file upload in php


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"; 
    } 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 

I am getting **

ftp_put(): Opening BINARY mode data connection

** this error and file not uploading.

People are also looking for solutions to the problem: php - Illegal string offset in array_map


Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.