Jump to content

downloads


PC Nerd

Recommended Posts

to force downloads this is the correct method as posted link from toplay

<?PHP
ob_start();
$fname = 'http://www.example.com/test.txt';

header('HTTP/1.1 200 OK');
header('Content-Type: application/unknown');
header('Content-Disposition: attachment; filename=' . basename($fname));
header('Content-Transfer-Encoding: binary');

readfile($fname);

header('Content-Length: ' . ob_get_length());

ob_end_flush();

echo 'This will not show in browser or be in file';
?>
Link to comment
https://forums.phpfreaks.com/topic/13419-downloads/#findComment-51833
Share on other sites

ok thanks.  can you walk me through wat the highlighted code does.  I dont quite understand wat some of the code does.  if you could that would be great

<quote>

<?PHP
[color=red]ob_start();[/color]
$fname = 'http://www.example.com/test.txt';

[color=red]header('HTTP/1.1 200 OK');
header('Content-Type: application/unknown');
header('Content-Disposition: attachment; filename=' . basename($fname));
header('Content-Transfer-Encoding: binary');

readfile($fname);

header('Content-Length: ' . ob_get_length());

ob_end_flush();[/color]
echo 'This will not show in browser or be in file';
?>

</quote>

i know it look really bad, but i always learn how to script things myself, not just copy others work.
Link to comment
https://forums.phpfreaks.com/topic/13419-downloads/#findComment-51840
Share on other sites

[code]ob_start();[/code]
Turns on 'Output buffering', meaning everything you intend to output, will be captured in an internal buffer instead of being directly outputted.

[code]header('HTTP/1.1 200 OK');
header('Content-Type: application/unknown');
header('Content-Disposition: attachment; filename=' . basename($fname));
header('Content-Transfer-Encoding: binary');[/code]
These are some HTTP/1.1 Protocol headers, you can read up on the HTTP/1.1 protcol here: [url=http://www.w3.org/Protocols/rfc2616/rfc2616.html]http://www.w3.org/Protocols/rfc2616/rfc2616.html[/url]

[code]readfile($fname);[/code]
Is a PHP function that reads a file directly into your script, binary-safe. In other words the contents of that file will be placed into your script.

[code]header('Content-Length: ' . ob_get_length());[/code]
This is another HTTP header, your sending over the length of your document, ob_get_length() returns the size of the internal output buffer in bytes.

[code]ob_end_flush();[/code]
This outputs what is currently in the internal buffer then turns off output buffering.

HTH, Zac.




Link to comment
https://forums.phpfreaks.com/topic/13419-downloads/#findComment-51847
Share on other sites

[quote author=PC Nerd link=topic=99144.msg390338#msg390338 date=1151813242]
ok so how does this get turned into the dowload.  i am reaonably new to php and dont quite get where the link to the file goes.

thanks for your help

Pc Nerd
[/quote]
When a user clicks on the link you did, then it will download the file. It's the simplest approach.

If you want to force a download to protect file location etc. then use code similar to what's provided. Please click on the link to the topic I provided and read it. It describes an approach (links.php & download.php). There's a link to a HTTP protocol book recommendation there too.

If you have the file saved locally, then use filesize() function to populate the Content-Length header and all the buffering stuff is not needed. It was used in that topic because the person had the file remotely using a full URL where filesize() doesn't work.

Good luck.
Link to comment
https://forums.phpfreaks.com/topic/13419-downloads/#findComment-51992
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.