squirrelly1
May 16th, 2005, 12:16 PM
how can I have my php script just send a file to be downloaded, ignoring the mime type (i suppose)
i'm trying to setup my server to allow people to download an mp3 (it's totally legal... btw) and the browser tries to play the mp3 instead of allowing it to be saved...
here is the code
<?php
function DownloadFile($file)
{
define('FILEDIR', '/music/');
$path = FILEDIR . $file;
//check that this file exists and that it doesn't include
//any special characters
if(!is_file($path) OR !eregi('^[A-Z_0-9][A-Z_0-9.]*$', $file))
{
print "Requested Path: $path<br>";
print "Is a File: " . is_file($path) . "<br>";
print "Is Valid: " . eregi('^[A-Z_0-9][A-Z_0-9.]*$', $file) . "<br>";
exit();
}
/*
** //check that the user has permission to download file
** if(user does not have permission)
** {
** //redirect to error page
** header("Location: error.php");
** exit();
** }
*/
$mimetype = array(
'doc'=>'application/msword',
'htm'=>'text/html',
'html'=>'text/html',
'jpg'=>'image/jpeg',
'pdf'=>'application/pdf',
'txt'=>'text/plain',
'xls'=>'application/vnd.ms-excel',
'mp3'=>'audio/mpeg'
);
$p = explode('.', $file);
$pc = count($p);
//send headers
if(($pc > 1) AND isset($mimetype[$p[$pc - 1]]))
{
//display file inside browser
header("Content-type: " . $mimetype[$p[$pc - 1]] . "\n");
}
else
{
//force download dialog
header("Content-type: application/octet-stream\n");
header("Content-disposition: attachment; filename=\"$file\"\n");
}
header("Content-transfer-encoding: binary\n");
header("Content-length: " . filesize($path) . "\n");
//send file contents
$fp=fopen($path, "r");
fpassthru($fp);
}
?>
thanks,
squirrelly1
i'm trying to setup my server to allow people to download an mp3 (it's totally legal... btw) and the browser tries to play the mp3 instead of allowing it to be saved...
here is the code
<?php
function DownloadFile($file)
{
define('FILEDIR', '/music/');
$path = FILEDIR . $file;
//check that this file exists and that it doesn't include
//any special characters
if(!is_file($path) OR !eregi('^[A-Z_0-9][A-Z_0-9.]*$', $file))
{
print "Requested Path: $path<br>";
print "Is a File: " . is_file($path) . "<br>";
print "Is Valid: " . eregi('^[A-Z_0-9][A-Z_0-9.]*$', $file) . "<br>";
exit();
}
/*
** //check that the user has permission to download file
** if(user does not have permission)
** {
** //redirect to error page
** header("Location: error.php");
** exit();
** }
*/
$mimetype = array(
'doc'=>'application/msword',
'htm'=>'text/html',
'html'=>'text/html',
'jpg'=>'image/jpeg',
'pdf'=>'application/pdf',
'txt'=>'text/plain',
'xls'=>'application/vnd.ms-excel',
'mp3'=>'audio/mpeg'
);
$p = explode('.', $file);
$pc = count($p);
//send headers
if(($pc > 1) AND isset($mimetype[$p[$pc - 1]]))
{
//display file inside browser
header("Content-type: " . $mimetype[$p[$pc - 1]] . "\n");
}
else
{
//force download dialog
header("Content-type: application/octet-stream\n");
header("Content-disposition: attachment; filename=\"$file\"\n");
}
header("Content-transfer-encoding: binary\n");
header("Content-length: " . filesize($path) . "\n");
//send file contents
$fp=fopen($path, "r");
fpassthru($fp);
}
?>
thanks,
squirrelly1