Forum Moderators: coopster

Message Too Old, No Replies

change file extension question

change file extension question

         

drooh

6:45 am on Apr 25, 2009 (gmt 0)

10+ Year Member



I've got files on the server that have a different extension than what they are, some files have no extension at all.

is there a way to have php translate them? for instance if I have a file 12345.htm but its really song.mp3 is there a way to write a script that will download 12345.htm as song.mp3?

g1smd

7:58 am on Apr 25, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



URLs are used on the web. Files are used internally within the server.

Requests to your server for URL 'A' can be intercepted by Mod_Rewrite and a suitable rewrite will pull the content from file 'B'. These rules can be crafted to cover just one file at a time, or with a suitable regular expression can deal with all URLs of a certain type/pattern. Note this is a rewrite, not a redirect.

The file extension part of the filename of the file on the server is used to trigger the action of adding a suitable MIME type as the content is served.

drooh

7:08 pm on Apr 27, 2009 (gmt 0)

10+ Year Member



thanks for the info, but I still have the same question.

is there a way to write a script that will download 12345.htm as song.mp3?

midtempo

10:34 pm on Apr 27, 2009 (gmt 0)

10+ Year Member



g1smd did answer your question, so this is just what he said, really.

you can make the server send song.mp3 when 12345.htm is requested. but php isn't what you need - you want to look at apache mod_rewrite to do this.

g1smd

10:49 pm on Apr 27, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Yes, check up on
Mod_Rewrite
code and the Apache forum at WebmasterWorld for many prior examples.

drooh

11:11 pm on Apr 27, 2009 (gmt 0)

10+ Year Member



im looking into this but may be a bit confusing.

I want to be able to click a link
<a href="download.php?file=random.htm">click</a>
and it pop up a window asking you to download songname.mp3 or files.zip. is there a preferred way to do this?

Here is another example. I am using a third party flash uploader which has an option to change the file name on the upload. it makes it some arbitrary file name and I think with no extension name at all. how are you supposed to work with something like that?

midtempo

10:03 am on Apr 28, 2009 (gmt 0)

10+ Year Member



> is there a preferred way to do this?

yes, with Mod_Rewrite.

you're wanting the server to send "file A" when someone asks for "file B". now, you could write a script in php that finds the correct file, reads it into memory, generates the right MIME Type (http://en.wikipedia.org/wiki/MIME_type) in the header and then outputs the file contents into the page, but this would be resource intensive and, imo, a really bad idea.

the simple, clean, fast way of doing this is by using Mod_Rewrite.

> how are you supposed to work with something like that?

by finding another upload script.