开发者

How to partially ftp a file (using ftp, wget with shell scripts or php)?

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

In php, h开发者_如何学Pythonttp://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.


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:password@example.com/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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜