developer
developer

Reputation: 2050

ftp_get giving error while fetching a file

Im trying to fetch a csv file from a remote server with ftp_get

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

$okk=0;
// try to download $server_file and save to $local_file
if (ftp_get($conn_id, $local_file, $server_file, FTP_BINARY))  (line 31)
{
        $okk=1;
}

but its giving following error

Warning: ftp_get(): Opening BINARY mode data connection for /abc/abc.csv(198528 bytes). in /home/a/b/c/cm_data/d.php on line 31

I tried changing it to ascii mode then too it gave error

Warning: ftp_get(): Opening ASCII mode data connection for /abc/abc.csv(198528 bytes). in /home/a/b/c/cm_data/d.php on line 31

i also tried using ftp_pasv($conn_id, TRUE); too but still gives error.

What is the problem please help!!

Upvotes: 0

Views: 3325

Answers (2)

Tudor Constantin
Tudor Constantin

Reputation: 26861

You seem not to treat error cases from ftp_connect and ftp_login.

Please try the following code and see if it gives some errors:

<?php

    $ftp_server = $ftp_server;
    $ftp_user = $ftp_user_name;
    $ftp_pass = $ftp_user_pass;

    // set up a connection or die
    $conn_id = ftp_connect($ftp_server) or die("Couldn't connect to $ftp_server"); 

    // try to login
    if (@ftp_login($conn_id, $ftp_user, $ftp_pass)) {
        echo "Connected as $ftp_user@$ftp_server\n";
        if (ftp_get($conn_id, $local_file, $server_file, FTP_BINARY)) {
            $okk=1;
        }
    } else {
        echo "Couldn't connect as $ftp_user\n";
    }

    // close the connection
    ftp_close($conn_id);  

?>

Upvotes: 0

user1509184
user1509184

Reputation: 41

ftp_pasv needs to be called after ftp_login!

Upvotes: 4

Related Questions