PDA

Click to See Complete Forum and Search --> : [resolved] ignore mime types


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

visualAd
May 16th, 2005, 04:09 PM
You just need to send an extra header to force it to download:

header('Content-Disposition: attachment; filename="nameoffile.ext"');

squirrelly1
May 16th, 2005, 04:26 PM
Yes... I had that


//force download dialog
header("Content-type: application/octet-stream\n");
header("Content-disposition: attachment; filename=\"$file\"\n");


but what I didn't have taken care of was the default player wanting to start up inside of the browser (quicktime pluggin)

so I needed to have another mime type in there for the audio files that was something like this

application/force-download

so that the browser wouldn't try to play it automatically...


Thanks,

squirrelly1

visualAd
May 16th, 2005, 04:40 PM
If I am not wrong you have the '.mp3' file extension in your array. Therefore it will play in the browser. To have it download simply remove the mp3 extension from your array.

CornedBee
May 16th, 2005, 05:37 PM
By the way, "readfile" is probably faster than "fopen+fpassthru".

squirrelly1
May 18th, 2005, 09:13 AM
I got it fixed with the thing I posted last time...

When I removed the .mp3 from the array, it would still include the file as part of the webpage and would have me download the whole thing (to include the html), which of course wouldn't work.

Thanks,

Squirrelly1