htaccess ForceType

I ran into a problem today that I did not think about. For an application I built, the user could upload pictures that would then get uploaded to a certain folder. For a couple of reasons, the filename used was not the original and it no longer contained the filename extension ( in this case jpg ). As I was testing the front end I ran into the problem doing things this way. When you pointed to the file on the server, it was treated like a text document and no image was seen. ...


There are usually many ways to deal with a problem and this one had a couple:

  • I could rewrite the backend so that it put the extension on the end of the filename. But then I would have to change all the currently uploaded files.
  • I could write a PHP script to parse the data and output the proper image type.
  • I could check and see if there was something to be done with htaccess.

The last idea on the list seemed the best. All the images ended up in their very own folder so I could set the rules for anything pulled from that folder. Also, it would require the least amount of changes to my previous code.

In fact it only took one line:
ForceType image/jpeg

And now all the files are processed as jpegs. I have tried this on Opera, Firefox and IE7 on windows and it works. I'll do some more checking tonight on linux but it seems to work quite well.