Page 1 of 1

Redirect to file

Posted: Thu Mar 25, 2004 11:14 pm
by Chris Corbyn
Hi,

I've got a page where a user has to input a code on a form to download a midi file. The form input is sent to a download.php page where if the code is correct the page should redirect to the file.

I've managed to use the header(location: ) idea but the problem is that the midi file opens in the browser when I need the download dialog to appear.

I've used

Code: Select all

header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=filename.ext");
too but this caused the file to be saved as a .php and also revealed the url to the file (unless I used the code incorrectly) because it offered the url incase the browser had made a mistake not knowing how to handle the file (netscape).
The redirect function needs to be placed inside a switch in an if command since there are many different files the page can redirect to.

Any idea how to make my redirect work so that the download dialog apppears?

Posted: Fri Mar 26, 2004 9:14 am
by Chris Corbyn
Hi,

Sorry, the problem with revealing the directory the files are located in is not a problem.

I've used this code

Code: Select all

header("Content-type: midi/force-download"); 
header("Content-Transfer-Encoding: Binary");  
header("Content-disposition: attachment; filename=contents/nokia/tones/Abs - Miss perfect.mid");
to redirect to a midi file located in contents/nokia/tones/ but the problem is that the download dialog does appear but wants to save a file called

"contents-nokia-tones-Abs - Miss perfect.mid.php"

When I download this file it is an empty file.


Am I using the header code incorrectly or something. it doesn't seem to be able to locate the file.

Thanks :-)

Posted: Fri Mar 26, 2004 9:23 am
by Chris Corbyn
Me again!!!!

I'll probably have figured this out by myself by the time anyone reads this... LOL

Just tried this piece of code from the manual

Code: Select all

header('Content-type: application/mid');

header('Content-Disposition: attachment; filename="Abs - Miss perfect.mid"');

readfile('contents/nokia/tones/Abs - Miss perfect.mid');
It works, the download is prompted and the data transferred is correct BUT it wants the save as a .php file not a .mid file?????

Any ideas?

Posted: Fri Mar 26, 2004 9:31 am
by Chris Corbyn
Told you I'd wor it out in the end.

Oh well, if anyone else ends up with the same problems here's the solution...

In the header line

Code: Select all

header('Content-type: Audio/Midi');
I previously specified content type as application/mid.

You need to make clear the contents type so that the browser know what to save it as. So a midi file is Audio type file which is a midi. You write it as shown above.

If for example you're saving a pdf file use application/pdf or a video sequence use video/mpeg. Ok got it.

Hope this might help somebody else some time.

Posted: Fri Mar 26, 2004 11:28 am
by pickle
Nothing new to add, but I appreciate you telling the community how you got it to work - congrats!