|
-
May 16th, 2005, 12:16 PM
#1
Thread Starter
Frenzied Member
[resolved] ignore mime types
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 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
Last edited by squirrelly1; May 18th, 2005 at 09:13 AM.
Now happily married and still crankin' away at the keyboard.  Life is grand for a coder, no?
-
May 16th, 2005, 04:09 PM
#2
Re: ignore mime types
You just need to send an extra header to force it to download:
PHP Code:
header('Content-Disposition: attachment; filename="nameoffile.ext"');
-
May 16th, 2005, 04:26 PM
#3
Thread Starter
Frenzied Member
Re: ignore mime types
Yes... I had that
PHP Code:
//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
Now happily married and still crankin' away at the keyboard.  Life is grand for a coder, no?
-
May 16th, 2005, 04:40 PM
#4
Re: ignore mime types
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.
-
May 16th, 2005, 05:37 PM
#5
Re: ignore mime types
By the way, "readfile" is probably faster than "fopen+fpassthru".
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
May 18th, 2005, 09:13 AM
#6
Thread Starter
Frenzied Member
Re: ignore mime types
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
Now happily married and still crankin' away at the keyboard.  Life is grand for a coder, no?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|