Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Download file from FTP using curl in php

Tags:

php

curl

ftp

I'm making a php script, intended to run in CLI, wich will help me to download files on a ftp server. I'm going to show you exactly what's wrong, but before i'll explain you what's the context.

I'm registred on a website that share oldies games image file. When we want to download a file on this site, we have to be logued to retrieve the download link. Then, the link given is an FTP download link.

I made a capture of the FTP connection packets using Wireshark, too compare it with my scripted FTP transfert that fails.

So,.. The FTP server accept anonymous connection. When i download a file using my browser, the link for the file is something like that;

ftp://website.url/somefolder/1/foo/bar/SOMEOLDIE.rar

Now, if i connect to this FTP using FileZilla, with anonymous login i get acces, i see some folders, but when i go to "somefolder" where my iso have to be, the folder is empty.

Look at this image, then i'll show you the code. enter image description here Now the code;

function download_file($url){
    echo "Downloading: ".$url;
    $ch = curl_init($url);
    $fp = fopen("data/output.rar", "w");
    curl_setopt($ch, CURLOPT_HEADER, 1);
    curl_setopt($curl, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)"); 
    curl_setopt($ch, CURLOPT_COOKIEFILE, 'data/cookies.txt');
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    //curl_setopt($ch, CURLOPT_FAILONERROR, 1);
    //curl_setopt($ch, CURLOPT_FTPLISTONLY, 1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
    curl_setopt($ch, CURLOPT_USERPWD, "anonymous:[email protected]");
    curl_setopt($ch, CURLOPT_FILE, $fp);
    curl_exec($ch);
    //var_dump(curl_getinfo($ch));
    //echo curl_error($ch);
    curl_close($ch);
    fclose($fp);
}

Can anyone help me with that ? I'm not familiar with curl, and i dont know how to tell curl to send exactly the commands i want.

Thank you !

EDIT: look at this, its exactly the same problem; Need help downloading a file using PHP and Curl from an FTP

like image 352
Math Avatar asked Oct 05 '22 10:10

Math


1 Answers

Ok guys i get it :)

I used the ftp_* functions for ftp, in place of curl, because curl tried to change directories, but its not allowed on this ftp server, so the code i used is;

$local_file = 'output.rar';
$server_file = '/somedir/1/bar/foo/somearchive.rar';
$ftp_user_name='anonymous';
$ftp_user_pass='[email protected]';
$ftp_server='server.host';
// 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);

/* uncomment if you need to change directories
if (ftp_chdir($conn_id, "<directory>")) {
    echo "Current directory is now: " . ftp_pwd($conn_id) . "\n";
} else { 
    echo "Couldn't change directory\n";
}
*/

// try to download $server_file and save to $local_file
if (ftp_get($conn_id, $local_file, $server_file, FTP_BINARY)) {
    echo "Successfully written to $local_file\n";
} else {
    echo "There was a problem\n";
}

// close the connection
ftp_close($conn_id);

It works !!

like image 101
Math Avatar answered Oct 12 '22 12:10

Math