Readfile not working properly

I am trying to upload a file through my server to me by streaming download.

This is my script:

header('Content-Description: File Transfer'); header('Content-Type: application/octet-stream'); header('Content-Disposition: attachment; filename=http://user: pass@example.com /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 is not what I really use, I just didn’t want to show the real URL. :) When I go to this file in my web browser, the download prompt does not appear and nothing happens. $r[2] is the length of the content in bytes of the file, and $fileName is the same URL as the attachment header. I really don't know what I'm doing wrong.

+4
source share
4 answers

ReadFile (); requires an absolute file path on the machine, not the URL.

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

+5
source

There is no file header at the end of the line ) , but I assume that this happened when editing the line.

For what it's worth, running your sample code gives a hint for downloading, in fact it hasn’t been tested very much, but maybe it’s something with the server / client configuration, not the code.

0
source

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

So edit

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

to

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

Hope you solve your problem.

0
source

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

I know this can be awkward, but sometimes we need workarounds to get the job done.

Try 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; 

it is tested and working; -)

0
source

All Articles