views:

138

answers:

2

hi, i want to partially download a ftp file. i just need to download lets say 10MB, but after skipping 100MB (for example).

In php, http://php.net/manual/en/function.ftp-fget.php this function allows arbitay starting point:

bool ftp_fget  (  resource $ftp_stream  ,  resource $handle  ,  string $remote_file  ,  int $mode  [,  int $resumepos = 0  ] )

however it does not allow me to set "how many bytes" i want to download.

+1  A: 

You should be able to open an FTP connection using a stream wrapper instead of the FTP functions (assuming you know the path to the file).

$fh = fopen('ftp://user:[email protected]/pub/file.txt', 'r');

Then just use normal file functions to open the file and read the information you want. You can't use fseek(), but fread() should work just fine.

Tim Lytle
A: 

You can pass the --range option to cURL to retrieve a range of bytes from a remote file.

If you have the curl command line tool installed you can run it from PHP using shell_exec() this way:

$bytes3to4 = shell_exec("curl --range 3-4 ftp://ftp.example.com/pub/file.txt");

Or if the cURL extension is available you can use it directly from PHP:

$ch = curl_init("ftp://ftp.example.com/pub/file.txt");

curl_setopt($ch, CURLOPT_RANGE, "3-4");  // fetch bytes 3 to 4
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);

$bytes3to4 = curl_exec($ch);

See the cURL manual for details

Alexandre Jasmin