View Full Version : fopen + fread only gets part of a remote txt file : (

06-11-2007, 07:58 AM
as the topic says, the following code returns 2 different things, the correct $filesize (9425) from the header and only part of the text file into $data (2491 bytes)

unfortunately, i cannot give out the user and pass. all i can say is that when i run it on my apache server locally, it reads the entire file properly. The split on the remote site occurs between the numbers 1 and 4 in "14", so i have no idea what trips it up.

$filename = 'http://user:pass@mysite.com/testfile.txt';
$ary_header = get_headers($filename, 1);
$filesize = $ary_header['Content-Length'];
$fh = fopen($filename, 'r');
$data = fread($fh, $filesize);
echo $filesize . "<br />";
echo strlen($data);

any ideas?

06-11-2007, 08:02 AM
You could try using file_get_contents instead. I think I remember something about fread timing out.

06-11-2007, 08:14 AM
this worked:

$filedata = file_get_contents('http://user:pass@mysite.com/testfile.txt');
echo $filedata;

i prefer 2 lines anyways :)