PDA

View Full Version : PHP/Bluehost - fetching a large remote file, how?



markowe
10-09-2009, 08:35 AM
Maybe this is a PHP question, but I am interested in this in the context of Bluehost too: how do I go about retrieving a large file, say 200Mb remotely via PHP, using FTP? I've never had to do this before, so am a newbie as far as this area of PHP goes.

Surely all the methods available in PHP (cUrl, fopen etc.) depend on reading the entire file into memory first, and then saving. This would time out my PHP thread long before the file was downloaded and probably cause a CPU/memory problem too, for a file that size, wouldn't it?

Is there a way to fetch a file via some kind of background thread on the server?

Thanks.

P.S. Is a cron job with wget the way forward? It's just I can't face learning yet another scripting language! If it can be done through PHP that would be a lot easier on me!

alexporter
10-20-2009, 05:47 AM
Hi,
Firstly you need to check do you have access to the file via ftp?

If so ftp in php isn't that difficult, here's an example


<?php
// set up basic connection ie ftp.example.com note no ftp://
$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";
}

// download a file
$download = ftp_get($conn_id, $local_file, $server_file, FTP_BINARY);

// check download status
if (!$download) {
echo "FTP download has failed!";
} else {
echo "Downloaded $source_file to $ftp_server as $destination_file";
}


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

Yes you could run it as a 'background thread' via a cron job once a day/week/etc using wget, do you need help with that?

hope that helps

alexporter
10-20-2009, 05:50 AM
Just thought of another way.

I dont know how to do this but I'm sure you can google it

You can read the file say 1mb at a time, then move the file pointer one and read the next 1mb