Link to home
Create AccountLog in
Avatar of hankknight
hankknightFlag for Canada

asked on

Most efficient way to check extension and send correct header

I use the following code to determine the correct content-type header to send, based on a file's extension.

It works the way I want it to, but I would like to know if it is the most resource friendly way to get the job done.

All suggestions are welcome, thanks!
<?php
$ext = strtolower(pathinfo($_GET['file'],PATHINFO_EXTENSION));
if ($ext=='323') header("Content-Type: text/h323");
elseif ($ext=='acx') header("Content-Type: application/internet-property-stream");
elseif ($ext=='ai') header("Content-Type: application/postscript");
elseif ($ext=='aif') header("Content-Type: audio/x-aiff");
elseif ($ext=='aifc') header("Content-Type: audio/x-aiff");
elseif ($ext=='aiff') header("Content-Type: audio/x-aiff");
elseif ($ext=='asf') header("Content-Type: video/x-ms-asf");
elseif ($ext=='asr') header("Content-Type: video/x-ms-asf");
elseif ($ext=='asx') header("Content-Type: video/x-ms-asf");
elseif ($ext=='au') header("Content-Type: audio/basic");
elseif ($ext=='avi') header("Content-Type: video/x-msvideo");
elseif ($ext=='axs') header("Content-Type: application/olescript");
elseif ($ext=='bas') header("Content-Type: text/plain");
elseif ($ext=='bcpio') header("Content-Type: application/x-bcpio");
elseif ($ext=='bin') header("Content-Type: application/octet-stream");
elseif ($ext=='bmp') header("Content-Type: image/bmp");
elseif ($ext=='c') header("Content-Type: text/plain");
elseif ($ext=='cat') header("Content-Type: application/vnd.ms-pkiseccat");
elseif ($ext=='cdf') header("Content-Type: application/x-cdf");
elseif ($ext=='cer') header("Content-Type: application/x-x509-ca-cert");
elseif ($ext=='class') header("Content-Type: application/octet-stream");
elseif ($ext=='clp') header("Content-Type: application/x-msclip");
elseif ($ext=='cmx') header("Content-Type: image/x-cmx");
elseif ($ext=='cod') header("Content-Type: image/cis-cod");
elseif ($ext=='cpio') header("Content-Type: application/x-cpio");
elseif ($ext=='crd') header("Content-Type: application/x-mscardfile");
elseif ($ext=='crl') header("Content-Type: application/pkix-crl");
elseif ($ext=='crt') header("Content-Type: application/x-x509-ca-cert");
elseif ($ext=='csh') header("Content-Type: application/x-csh");
elseif ($ext=='css') header("Content-Type: text/css");
elseif ($ext=='dcr') header("Content-Type: application/x-director");
elseif ($ext=='der') header("Content-Type: application/x-x509-ca-cert");
elseif ($ext=='dir') header("Content-Type: application/x-director");
elseif ($ext=='dll') header("Content-Type: application/x-msdownload");
elseif ($ext=='dms') header("Content-Type: application/octet-stream");
elseif ($ext=='doc') header("Content-Type: application/msword");
elseif ($ext=='dot') header("Content-Type: application/msword");
elseif ($ext=='dvi') header("Content-Type: application/x-dvi");
elseif ($ext=='dxr') header("Content-Type: application/x-director");
elseif ($ext=='eps') header("Content-Type: application/postscript");
elseif ($ext=='etx') header("Content-Type: text/x-setext");
elseif ($ext=='evy') header("Content-Type: application/envoy");
elseif ($ext=='exe') header("Content-Type: application/octet-stream");
elseif ($ext=='fif') header("Content-Type: application/fractals");
elseif ($ext=='flr') header("Content-Type: x-world/x-vrml");
elseif ($ext=='gif') header("Content-Type: image/gif");
elseif ($ext=='gtar') header("Content-Type: application/x-gtar");
elseif ($ext=='gz') header("Content-Type: application/x-gzip");
elseif ($ext=='h') header("Content-Type: text/plain");
elseif ($ext=='hdf') header("Content-Type: application/x-hdf");
elseif ($ext=='hlp') header("Content-Type: application/winhlp");
elseif ($ext=='hqx') header("Content-Type: application/mac-binhex40");
elseif ($ext=='hta') header("Content-Type: application/hta");
elseif ($ext=='htc') header("Content-Type: text/x-component");
elseif ($ext=='htm') header("Content-Type: text/html");
elseif ($ext=='html') header("Content-Type: text/html");
elseif ($ext=='htt') header("Content-Type: text/webviewhtml");
elseif ($ext=='ico') header("Content-Type: image/x-icon");
elseif ($ext=='ief') header("Content-Type: image/ief");
elseif ($ext=='iii') header("Content-Type: application/x-iphone");
elseif ($ext=='ins') header("Content-Type: application/x-internet-signup");
elseif ($ext=='isp') header("Content-Type: application/x-internet-signup");
elseif ($ext=='jfif') header("Content-Type: image/pipeg");
elseif ($ext=='jpe') header("Content-Type: image/jpeg");
elseif ($ext=='jpeg') header("Content-Type: image/jpeg");
elseif ($ext=='jpg') header("Content-Type: image/jpeg");
elseif ($ext=='js') header("Content-Type: application/x-javascript");
elseif ($ext=='latex') header("Content-Type: application/x-latex");
elseif ($ext=='lha') header("Content-Type: application/octet-stream");
elseif ($ext=='lsf') header("Content-Type: video/x-la-asf");
elseif ($ext=='lsx') header("Content-Type: video/x-la-asf");
elseif ($ext=='lzh') header("Content-Type: application/octet-stream");
elseif ($ext=='m13') header("Content-Type: application/x-msmediaview");
elseif ($ext=='m14') header("Content-Type: application/x-msmediaview");
elseif ($ext=='m3u') header("Content-Type: audio/x-mpegurl");
elseif ($ext=='man') header("Content-Type: application/x-troff-man");
elseif ($ext=='mdb') header("Content-Type: application/x-msaccess");
elseif ($ext=='me') header("Content-Type: application/x-troff-me");
elseif ($ext=='mht') header("Content-Type: message/rfc822");
elseif ($ext=='mhtml') header("Content-Type: message/rfc822");
elseif ($ext=='mid') header("Content-Type: audio/mid");
elseif ($ext=='mny') header("Content-Type: application/x-msmoney");
elseif ($ext=='mov') header("Content-Type: video/quicktime");
elseif ($ext=='movie') header("Content-Type: video/x-sgi-movie");
elseif ($ext=='mp2') header("Content-Type: video/mpeg");
elseif ($ext=='mp3') header("Content-Type: audio/mpeg");
elseif ($ext=='mpa') header("Content-Type: video/mpeg");
elseif ($ext=='mpe') header("Content-Type: video/mpeg");
elseif ($ext=='mpeg') header("Content-Type: video/mpeg");
elseif ($ext=='mpg') header("Content-Type: video/mpeg");
elseif ($ext=='mpp') header("Content-Type: application/vnd.ms-project");
elseif ($ext=='mpv2') header("Content-Type: video/mpeg");
elseif ($ext=='ms') header("Content-Type: application/x-troff-ms");
elseif ($ext=='mvb') header("Content-Type: application/x-msmediaview");
elseif ($ext=='nws') header("Content-Type: message/rfc822");
elseif ($ext=='oda') header("Content-Type: application/oda");
elseif ($ext=='p10') header("Content-Type: application/pkcs10");
elseif ($ext=='p12') header("Content-Type: application/x-pkcs12");
elseif ($ext=='p7b') header("Content-Type: application/x-pkcs7-certificates");
elseif ($ext=='p7c') header("Content-Type: application/x-pkcs7-mime");
elseif ($ext=='p7m') header("Content-Type: application/x-pkcs7-mime");
elseif ($ext=='p7r') header("Content-Type: application/x-pkcs7-certreqresp");
elseif ($ext=='p7s') header("Content-Type: application/x-pkcs7-signature");
elseif ($ext=='pbm') header("Content-Type: image/x-portable-bitmap");
elseif ($ext=='pdf') header("Content-Type: application/pdf");
elseif ($ext=='pfx') header("Content-Type: application/x-pkcs12");
elseif ($ext=='pgm') header("Content-Type: image/x-portable-graymap");
elseif ($ext=='pko') header("Content-Type: application/ynd.ms-pkipko");
elseif ($ext=='pma') header("Content-Type: application/x-perfmon");
elseif ($ext=='pmc') header("Content-Type: application/x-perfmon");
elseif ($ext=='pml') header("Content-Type: application/x-perfmon");
elseif ($ext=='pmr') header("Content-Type: application/x-perfmon");
elseif ($ext=='pmw') header("Content-Type: application/x-perfmon");
elseif ($ext=='pnm') header("Content-Type: image/x-portable-anymap");
elseif ($ext=='pot,') header("Content-Type: application/vnd.ms-powerpoint");
elseif ($ext=='ppm') header("Content-Type: image/x-portable-pixmap");
elseif ($ext=='pps') header("Content-Type: application/vnd.ms-powerpoint");
elseif ($ext=='ppt') header("Content-Type: application/vnd.ms-powerpoint");
elseif ($ext=='prf') header("Content-Type: application/pics-rules");
elseif ($ext=='ps') header("Content-Type: application/postscript");
elseif ($ext=='pub') header("Content-Type: application/x-mspublisher");
elseif ($ext=='qt') header("Content-Type: video/quicktime");
elseif ($ext=='ra') header("Content-Type: audio/x-pn-realaudio");
elseif ($ext=='ram') header("Content-Type: audio/x-pn-realaudio");
elseif ($ext=='ras') header("Content-Type: image/x-cmu-raster");
elseif ($ext=='rgb') header("Content-Type: image/x-rgb");
elseif ($ext=='rmi') header("Content-Type: audio/mid");
elseif ($ext=='roff') header("Content-Type: application/x-troff");
elseif ($ext=='rtf') header("Content-Type: application/rtf");
elseif ($ext=='rtx') header("Content-Type: text/richtext");
elseif ($ext=='scd') header("Content-Type: application/x-msschedule");
elseif ($ext=='sct') header("Content-Type: text/scriptlet");
elseif ($ext=='setpay') header("Content-Type: application/set-payment-initiation");
elseif ($ext=='setreg') header("Content-Type: application/set-registration-initiation");
elseif ($ext=='sh') header("Content-Type: application/x-sh");
elseif ($ext=='shar') header("Content-Type: application/x-shar");
elseif ($ext=='sit') header("Content-Type: application/x-stuffit");
elseif ($ext=='snd') header("Content-Type: audio/basic");
elseif ($ext=='spc') header("Content-Type: application/x-pkcs7-certificates");
elseif ($ext=='spl') header("Content-Type: application/futuresplash");
elseif ($ext=='src') header("Content-Type: application/x-wais-source");
elseif ($ext=='sst') header("Content-Type: application/vnd.ms-pkicertstore");
elseif ($ext=='stl') header("Content-Type: application/vnd.ms-pkistl");
elseif ($ext=='stm') header("Content-Type: text/html");
elseif ($ext=='svg') header("Content-Type: image/svg+xml");
elseif ($ext=='sv4cpio') header("Content-Type: application/x-sv4cpio");
elseif ($ext=='sv4crc') header("Content-Type: application/x-sv4crc");
elseif ($ext=='swf') header("Content-Type: application/x-shockwave-flash");
elseif ($ext=='t') header("Content-Type: application/x-troff");
elseif ($ext=='tar') header("Content-Type: application/x-tar");
elseif ($ext=='tcl') header("Content-Type: application/x-tcl");
elseif ($ext=='tex') header("Content-Type: application/x-tex");
elseif ($ext=='texi') header("Content-Type: application/x-texinfo");
elseif ($ext=='texinfo') header("Content-Type: application/x-texinfo");
elseif ($ext=='tgz') header("Content-Type: application/x-compressed");
elseif ($ext=='tif') header("Content-Type: image/tiff");
elseif ($ext=='tiff') header("Content-Type: image/tiff");
elseif ($ext=='tr') header("Content-Type: application/x-troff");
elseif ($ext=='trm') header("Content-Type: application/x-msterminal");
elseif ($ext=='tsv') header("Content-Type: text/tab-separated-values");
elseif ($ext=='txt') header("Content-Type: text/plain");
elseif ($ext=='uls') header("Content-Type: text/iuls");
elseif ($ext=='ustar') header("Content-Type: application/x-ustar");
elseif ($ext=='vcf') header("Content-Type: text/x-vcard");
elseif ($ext=='vrml') header("Content-Type: x-world/x-vrml");
elseif ($ext=='wav') header("Content-Type: audio/x-wav");
elseif ($ext=='wcm') header("Content-Type: application/vnd.ms-works");
elseif ($ext=='wdb') header("Content-Type: application/vnd.ms-works");
elseif ($ext=='wks') header("Content-Type: application/vnd.ms-works");
elseif ($ext=='wmf') header("Content-Type: application/x-msmetafile");
elseif ($ext=='wps') header("Content-Type: application/vnd.ms-works");
elseif ($ext=='wri') header("Content-Type: application/x-mswrite");
elseif ($ext=='wrl') header("Content-Type: x-world/x-vrml");
elseif ($ext=='wrz') header("Content-Type: x-world/x-vrml");
elseif ($ext=='xaf') header("Content-Type: x-world/x-vrml");
elseif ($ext=='xbm') header("Content-Type: image/x-xbitmap");
elseif ($ext=='xla') header("Content-Type: application/vnd.ms-excel");
elseif ($ext=='xlc') header("Content-Type: application/vnd.ms-excel");
elseif ($ext=='xlm') header("Content-Type: application/vnd.ms-excel");
elseif ($ext=='xls') header("Content-Type: application/vnd.ms-excel");
elseif ($ext=='xlt') header("Content-Type: application/vnd.ms-excel");
elseif ($ext=='xlw') header("Content-Type: application/vnd.ms-excel");
elseif ($ext=='xof') header("Content-Type: x-world/x-vrml");
elseif ($ext=='xpm') header("Content-Type: image/x-xpixmap");
elseif ($ext=='xwd') header("Content-Type: image/x-xwindowdump");
elseif ($ext=='z') header("Content-Type: application/x-compress");
elseif ($ext=='zip') header("Content-Type: application/zip");
else header("Content-Type: application/force-download"); 
?>

Open in new window

Avatar of Sekurity863
Sekurity863
Flag of United States of America image

try using switch/case
ASKER CERTIFIED SOLUTION
Avatar of Sekurity863
Sekurity863
Flag of United States of America image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
If you are curious as to which is faster, you can see a side-by-side comparison here:
http://rob.sun3.org/php-code/switch-vs-if/
Hello hankknight,

Have you considered using an existing class like http://phpclasses.fonant.com/browse/package/5082.html

Regards,

alain34
why not build/use an array, would reduce the code amount, look neater, and you only have 1 header() call..

and finishing building the array of course.
$ext = strtolower(pathinfo($_GET['file'],PATHINFO_EXTENSION));
 
$mimeTimes = array (
		"323" => "text/h323",
		"acx" => "application/internet-property-stream",
		"ai" => "application/postscript",
		"force" => "application/force-download"
	);
	
header("Content-Type: " . (array_key_exists($ext, $mimeTimes) === true) ? $mimeTimes[$ext] : $mimeTimes["force"]);

Open in new window

sorry, $mimeTimes should have been $mimeTypes... have no idea where "Times" came from.
Avatar of hankknight

ASKER

MMDeveloper, your idea saves many lines of code however switch/case seems to perform faster.
Switch cases are amazing. I use them whenever I start having a lot of if/then/else conditions. Very helpful and easy to add more cases in the future.

Good luck with your endeavor hankknight, hope I was helpful!

-s863
SOLUTION
Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Very good point ray
@Sekurity863, thanks - agree with your point about switch / case / default.

Logrolling via the EE forums!

;-)

Ray