Jump to content

Downloading first n bytes of remote file

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
2 replies to this topic

#1
elle

elle

    Newbie

  • Members
  • PipPip
  • 10 posts
Hi there,

I need to download FIRST n bytes (n ~ 1024+^2) from remote file (located on the other server than mine). All must be done via fopen or similar functions and served to user via header function in binary mode. No redirection (Location via header allowed !

How's the best technique of that ?

It should be look like the code below. This code I'm using to retrieve data from the tail of the file :


<?php

	

	@ignore_user_abort();

	@set_time_limit(0);


	$name ='http://www.remoteserver.org/file.dat';

	$part_size = 1048576;


	$fp = @fopen($name, 'rb');

	

	@fseek($fp, -$part_size, SEEK_END);


	header("Content-Type: application/octet-stream");

	header('Content-Disposition: attachment; filename="file_part.dat"');

	header("Content-Length: ". $part_size);

	

	@fpassthru($fp);

	exit;


?>


TIA4NFO

#2
Lop

Lop

    Speaks fluent binary

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,172 posts
I'm assuming you know n so you could set the header("size: ") to the answer of your equation.

P.S. - it may be "file-size"?

#3
Surgeon

Surgeon

    Newbie

  • Members
  • Pip
  • 3 posts
You could use fgets to specify how many bytes to read from the file. May want to look into that :)

check out the PHP manual for fgets()