Web Content: Forcing Browser Downloads


Web Content Tips - Part 5: If you want to send a file and manually trigger its download mechanism, you can modify the header. We send the browser appropriate Content-Type and Content-Disposition headers. Look this sample:
<?php
$file = "test.txt";

header("Content-Type: plain/text");
header("Content-Length: " . filesize($file));
header("Content-Disposition: attachment; filename=".basename($file));

readfile($file);

?>

Another sample:

<?php
$file = "test.zip";

header("Content-Type: application/octet-stream");
header("Content-Length: " . filesize($file));
header("Content-Disposition: attachment; filename=".basename($file));

readfile($file);

?>



Bookmark and Share Tag: web content, browser, download Category: PHP Basic Post : April 28th 2008 Read: 2,238

advertisements


blog comments powered by Disqus