note 60711 added to function.fread
| From: | junk at gieson dot com | Date: | Sat, 14 Jan 2006 22:40:21 +0000 |
| Subject: | note 60711 added to function.fread | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-101767@lists.php.net to get a copy of this message | ||
To force download an mp3 file and/or prevent the mp3 from ending up in the browser's cache.
This is very similiar to mindplay's example below.
<?php
@ob_end_clean();
// Only allow mp3 files
$allowedFileType = "mp3";
// Set the filename based on the URL's query string
$theFile = $_REQUEST['theFile'];
// Get info about the file
$f = pathinfo($theFile);
// Check the extension against allowed file types
if(strtolower($f['extension']) != strtolower($allowedFileType)) exit;
// Make sure the file exists
if (!file_exists($theFile)) exit;
// Set headers
header("Pragma: public");
header("Expires: Thu, 19 Nov 1981 08:52:00 GMT");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: private");
header("Content-Transfer-Encoding: binary");
header("Content-Type: audio/x-mpeg, audio/x-mpeg-3, audio/mpeg3");
// This line causes the browser's "save as" dialog
header( 'Content-Disposition: attachment;
filename="'.$f['basename'].'"' );
// Length required for Internet Explorer
header("Content-Length: ".@urldecode(@filesize($theFile)));
// Open file
if (($f = fopen($theFile, 'rb')) === false) exit;
// Push file
while (!feof($f)) {
echo fread($f, (1*(1024*1024)));
flush();
@ob_flush();
}
// Close file
fclose($f);
exit;
?>
----
Manual Page -- http://www.php.net/manual/en/function.fread.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+60711
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+60711&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+60711&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+60711&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+60711&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+60711&report=yes&reason=non-english
Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+60711&report=yes&reason=already+in+docs
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+60711&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+60711&report=yes
Search -- http://master.php.net/manage/user-notes.php