View Full Version : generic download script
brothercake
08-21-2002, 11:37 PM
i made this, which just gets a filename and path from a query string and prompts for download
$saveName = stripslashes($HTTP_GET_VARS["name"]);
$savePath = stripslashes($HTTP_GET_VARS["file"]);
header ("Content-Type: application/octet-stream");
header ("Content-Disposition: attachment; filename=$saveName");
readfile($savePath);
and it works with text files, but i use it for wavs or mp3 then the resultant file is empty
Can anyone advise?
_Spud_
08-23-2002, 11:34 AM
Originally posted by brothercake
i made this, which just gets a filename and path from a query string and prompts for download
$saveName = stripslashes($HTTP_GET_VARS["name"]);
$savePath = stripslashes($HTTP_GET_VARS["file"]);
header ("Content-Type: application/octet-stream");
header ("Content-Disposition: attachment; filename=$saveName");
readfile($savePath);
and it works with text files, but i use it for wavs or mp3 then the resultant file is empty
Can anyone advise?
I am no expert on php, so this may be wrong. but if you add this line of code after your header commands it MAY work
header("Content-Transfer-Encoding: ascii");
brothercake
08-23-2002, 11:43 PM
Well I wanted it to work with binary files, but I didn't know there was a header for transfer encoding. So I set it to binary, and then it was fine.
thanks :)
_Spud_
08-24-2002, 12:52 AM
i am glad it works.
I need something like this for my site, can you tell me how you activate the script?
brothercake
08-24-2002, 04:11 AM
Well, the final script looked like this
<?
$saveName = stripslashes($HTTP_GET_VARS["name"]);
$savePath = stripslashes($HTTP_GET_VARS["path"]);
header ("Content-Type: application/octet-stream");
header ("Content-Disposition: attachment; filename=$saveName");
header ("Content-Transfer-Encoding: binary");
readfile($savePath);
?>
and then send it a query string with the path and filename, like
<a href="/download.php?path=music/hifi/dropit.mp3&name=dropit.mp3">
Meggie
11-19-2003, 07:49 PM
Hi, I saw these codes and think they're very helpful for me. However, I am using Java in jsp. I don't understand this: readfile(filePath); I am trying to figure out what is the similar API in java, not PHP. Can somebody please help ?
Also, I have another question. Is there a way to force users to use certain file type (for example, .csv ) rather than let them type in whatever they want ? ( For example, on UI, we can at least use Javascript to alert users to enter certain file type, but here, in http protocol (octet-stream) it's not in our control. How can we alert them to use the file type that we want ? Thank you so much for any help.
missing-score
11-20-2003, 11:35 AM
1. I have never looked at jsp at all, so im afraid i cant help.
2. Yes, you can stop them entering just any file type. Once the request has been sent there isn't alot you can do, but before, all you have to do is check the filename input, and check it is an allowed type.
Socraties
11-21-2003, 11:00 PM
Meggie -
I would imagine that you could use something like a try-catch statement. Once you get the file string you are downloading you could check the extension and if that extension isn't within the specified file extensions you would like then throw and exception. Hope this helps you some.
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.