Support Forums
[Function] Force download of a file - Printable Version

+- Support Forums (https://www.supportforums.net)
+-- Forum: Categories (https://www.supportforums.net/forumdisplay.php?fid=87)
+--- Forum: Coding Support Forums (https://www.supportforums.net/forumdisplay.php?fid=18)
+---- Forum: PHP The Hypertext Preprocessor (https://www.supportforums.net/forumdisplay.php?fid=21)
+---- Thread: [Function] Force download of a file (/showthread.php?tid=4942)



[Function] Force download of a file - OX!DE - 02-21-2010

Coded by me, forces the default save dialog prompt to appear. Good if you want to hide a files original source.

PHP Code:
<?php
ob_start
();
// Example Usage: downloadfile("meandyourmom.png");
function downloadfile($file2d){
@
ob_clean(); 
@
$fh=fopen($file2d,'r');
@
$buffer=fread($fh,filesize($file2d));
header("Content-type: application/octet-stream");
header("Content-length: ".strlen($buffer));
header("Content-disposition: attachment; filename=".basename($file2d).';');
@
ob_get_clean();
echo 
$buffer;
@
fclose($fh);
@
ob_clean(); 
}
?>

You could also do something like unlink the file afterwards so they can't download it again.


RE: [Function] Force download of a file - h1r0n - 02-25-2010

You can also use this to save a file to your server