Reputation: 15683
It is common to server downloadable files which are located outside the public folder by PHP:
$fp = fopen($file,"r") ;
header("Content-Type: application/msword");
header('Content-Disposition: attachment; filename="'.$filename.'"');
while (! feof($fp)) {
$buff = fread($fp,4096);
echo $buff;
}
I have two questions:
Comparing with static files served by web server (direct file URL); does this method needs more resources (memory and cpu), as PHP needs to read the file and deliver?
How we can display some text on the page? As we defined header
, we do not have html output of the php script.
Upvotes: 2
Views: 1752
Reputation: 4670
Also I'd suggest using the Content Length Header as well.
header('Content-Length: '. filesize($path));
Upvotes: 3
Reputation: 6190
1 Since you have to process the file with this script it require more resources than just normal download link. However this is depend on your needs. If you think these files need more security. Let's say only authenticated users can download the file and only the file belongs to him. Then you need to validate those. In such situation you need the code you have put in your question. If your files are open to the public then you can display direct link to the file may be locating them somewhere in public temporarily.
2 I can suggest you two methods to perform this.
Method 1 :
You need javascript support to perform this kind of requirement in handy way. Assume you need to display some HTML on the page where the download is possible. You can create a page with the HTML you want and you can put a download button.
<input type="button" name="cmdDownload" id="cmdDownload" value="Download" onclick="downloadFile('<?php echo $pathToTheFile; ?>');" />
And you can keep hidden iframe to process the download.
<iframe id="downloadFrame" style="display:none"></iframe>
Assume your PHP download page is download.php.
Then you can have a javascript function like this.
<script type="text/javascript">
function downloadFile(filepath)
{
var ifrme = document.getElementById("downloadFrame");
ifrme.src = "download.php?filepath="+filepath;
}
</script>
Method 2:
Other than above method you can use META Refresh as well.
<meta http-equiv="Refresh" content="3;URL=<?php echo $fullHTTPathToYourFile ?>" />
You can have HTML display with this too.
Upvotes: 2
Reputation: 47034
If output buffering is active, serving a file using PHP will consume a lot of memory, and performance will be much worse than in the case of regular files served by apache.
So to add something to the answers already given:
ob_end_flush()
, so the file contents will not be cached in memory before starting outputreadfile()
might stream a file from disk to the web more efficiently than your loopIt has been said but cannot be stressed enough: do include the Content-Length
header, since it will allow the downloader to measure progress.
Upvotes: 1
Reputation: 360672
<meta>
(or javascript) redirect to forward the user to the actual download url.Upvotes: 2