Joseph
Joseph

Reputation: 159

readfile not working properly

I'm trying to download a file through my server to me, by streaming the download.

This is my script:

header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename=http://user:[email protected]/file.bin';
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . $r[2]);
ob_clean();
flush();
readfile($fileName);
exit;

example.com isn't what I'm really using, I just didn't want to show the real URL. :) When I go to this file in my web browser a download prompt does not come up and nothing happens. $r[2] is the content length in bytes of the file and $fileName is the same URL as used in the attachment header. I really don't know what I'm doing wrong.

Upvotes: 4

Views: 12354

Answers (4)

Tom
Tom

Reputation: 1115

readfile(); needs the absolute path of the file on the machine rather than a url.

e.g. /home/person/file.exe

Upvotes: 5

Junior Mayhe
Junior Mayhe

Reputation: 16401

I found a way to download an image for example, but this could help with any kind of file. It downloads a file without using readfile.

I know it can be clumsy but sometimes we need some workarounds to get the job done.

Try this, where $src = 'http://external-server/remote-image.jpg'

$url_stuff = parse_url($src); 
$port = isset($url_stuff['port']) ? $url_stuff['port'] : 80; 
$fp = fsockopen($url_stuff['host'], $port); 
$query  = 'GET ' . $url_stuff['path'] . " HTTP/1.0\n"; 
$query .= 'Host: ' . $url_stuff['host']; 
$query .= "\n\n"; 
$buffer=null;
fwrite($fp, $query); 
while ($tmp = fread($fp, 1024)) 
{ 
    $buffer .= $tmp; 
} 

preg_match('/Content-Length: ([0-9]+)/', $buffer, $parts); 
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename=your_file.bin');
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');

$content = substr($buffer, - $parts[1]); 
echo $content;

this is tested and working ;-)

Upvotes: 0

eldblz
eldblz

Reputation: 808

IMHO the problem is the filesize, try to get the filesize over http (look at this function): http://codesnippets.joyent.com/posts/show/1214

So edit from

header('Content-Length: ' . $r[2]);

to

header('Content-Length: ' . get_remote_file_size($fileurl) );

Hope you solve your issue.

Upvotes: 0

Tim Lytle
Tim Lytle

Reputation: 17624

There's a missing ) at the end of the filename header line, but I'm assuming that happened when edited the line.

For what it's worth, running your sample code on gives a download prompt, haven't really tested it much past that, but maybe it's something with the server/client configuration - not the code.

Upvotes: 0

Related Questions