Redirect to file

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Redirect to file

Post 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?
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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 :-)
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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?
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

Nothing new to add, but I appreciate you telling the community how you got it to work - congrats!
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply