File.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656
  1. <?php
  2. namespace Symfony\Component\HttpFoundation\File;
  3. use Symfony\Component\HttpFoundation\File\Exception\FileException;
  4. use Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException;
  5. use Symfony\Component\HttpFoundation\File\MimeType\MimeTypeGuesser;
  6. /*
  7. * This file is part of the symfony package.
  8. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  9. *
  10. * For the full copyright and license information, please view the LICENSE
  11. * file that was distributed with this source code.
  12. */
  13. /**
  14. * A file in the file system
  15. *
  16. * @author Bernhard Schussek <bernhard.schussek@symfony-project.com>
  17. */
  18. class File
  19. {
  20. /**
  21. * Assignment of mime types to their default extensions
  22. * @var array
  23. */
  24. static protected $defaultExtensions = array(
  25. 'application/andrew-inset' => 'ez',
  26. 'application/appledouble' => 'base64',
  27. 'application/applefile' => 'base64',
  28. 'application/commonground' => 'dp',
  29. 'application/cprplayer' => 'pqi',
  30. 'application/dsptype' => 'tsp',
  31. 'application/excel' => 'xls',
  32. 'application/font-tdpfr' => 'pfr',
  33. 'application/futuresplash' => 'spl',
  34. 'application/hstu' => 'stk',
  35. 'application/hyperstudio' => 'stk',
  36. 'application/javascript' => 'js',
  37. 'application/mac-binhex40' => 'hqx',
  38. 'application/mac-compactpro' => 'cpt',
  39. 'application/mbed' => 'mbd',
  40. 'application/mirage' => 'mfp',
  41. 'application/msword' => 'doc',
  42. 'application/ocsp-request' => 'orq',
  43. 'application/ocsp-response' => 'ors',
  44. 'application/octet-stream' => 'bin',
  45. 'application/oda' => 'oda',
  46. 'application/ogg' => 'ogg',
  47. 'application/pdf' => 'pdf',
  48. 'application/x-pdf' => 'pdf',
  49. 'application/pgp-encrypted' => '7bit',
  50. 'application/pgp-keys' => '7bit',
  51. 'application/pgp-signature' => 'sig',
  52. 'application/pkcs10' => 'p10',
  53. 'application/pkcs7-mime' => 'p7m',
  54. 'application/pkcs7-signature' => 'p7s',
  55. 'application/pkix-cert' => 'cer',
  56. 'application/pkix-crl' => 'crl',
  57. 'application/pkix-pkipath' => 'pkipath',
  58. 'application/pkixcmp' => 'pki',
  59. 'application/postscript' => 'ps',
  60. 'application/presentations' => 'shw',
  61. 'application/prs.cww' => 'cw',
  62. 'application/prs.nprend' => 'rnd',
  63. 'application/quest' => 'qrt',
  64. 'application/rtf' => 'rtf',
  65. 'application/sgml-open-catalog' => 'soc',
  66. 'application/sieve' => 'siv',
  67. 'application/smil' => 'smi',
  68. 'application/toolbook' => 'tbk',
  69. 'application/vnd.3gpp.pic-bw-large' => 'plb',
  70. 'application/vnd.3gpp.pic-bw-small' => 'psb',
  71. 'application/vnd.3gpp.pic-bw-var' => 'pvb',
  72. 'application/vnd.3gpp.sms' => 'sms',
  73. 'application/vnd.acucorp' => 'atc',
  74. 'application/vnd.adobe.xfdf' => 'xfdf',
  75. 'application/vnd.amiga.amu' => 'ami',
  76. 'application/vnd.blueice.multipass' => 'mpm',
  77. 'application/vnd.cinderella' => 'cdy',
  78. 'application/vnd.cosmocaller' => 'cmc',
  79. 'application/vnd.criticaltools.wbs+xml' => 'wbs',
  80. 'application/vnd.curl' => 'curl',
  81. 'application/vnd.data-vision.rdz' => 'rdz',
  82. 'application/vnd.dreamfactory' => 'dfac',
  83. 'application/vnd.fsc.weblauch' => 'fsc',
  84. 'application/vnd.genomatix.tuxedo' => 'txd',
  85. 'application/vnd.hbci' => 'hbci',
  86. 'application/vnd.hhe.lesson-player' => 'les',
  87. 'application/vnd.hp-hpgl' => 'plt',
  88. 'application/vnd.ibm.electronic-media' => 'emm',
  89. 'application/vnd.ibm.rights-management' => 'irm',
  90. 'application/vnd.ibm.secure-container' => 'sc',
  91. 'application/vnd.ipunplugged.rcprofile' => 'rcprofile',
  92. 'application/vnd.irepository.package+xml' => 'irp',
  93. 'application/vnd.jisp' => 'jisp',
  94. 'application/vnd.kde.karbon' => 'karbon',
  95. 'application/vnd.kde.kchart' => 'chrt',
  96. 'application/vnd.kde.kformula' => 'kfo',
  97. 'application/vnd.kde.kivio' => 'flw',
  98. 'application/vnd.kde.kontour' => 'kon',
  99. 'application/vnd.kde.kpresenter' => 'kpr',
  100. 'application/vnd.kde.kspread' => 'ksp',
  101. 'application/vnd.kde.kword' => 'kwd',
  102. 'application/vnd.kenameapp' => 'htke',
  103. 'application/vnd.kidspiration' => 'kia',
  104. 'application/vnd.kinar' => 'kne',
  105. 'application/vnd.llamagraphics.life-balance.desktop' => 'lbd',
  106. 'application/vnd.llamagraphics.life-balance.exchange+xml' => 'lbe',
  107. 'application/vnd.lotus-1-2-3' => 'wks',
  108. 'application/vnd.mcd' => 'mcd',
  109. 'application/vnd.mfmp' => 'mfm',
  110. 'application/vnd.micrografx.flo' => 'flo',
  111. 'application/vnd.micrografx.igx' => 'igx',
  112. 'application/vnd.mif' => 'mif',
  113. 'application/vnd.mophun.application' => 'mpn',
  114. 'application/vnd.mophun.certificate' => 'mpc',
  115. 'application/vnd.mozilla.xul+xml' => 'xul',
  116. 'application/vnd.ms-artgalry' => 'cil',
  117. 'application/vnd.ms-asf' => 'asf',
  118. 'application/vnd.ms-excel' => 'xls',
  119. 'application/vnd.ms-excel.sheet.macroenabled.12' => 'xlsm',
  120. 'application/vnd.ms-lrm' => 'lrm',
  121. 'application/vnd.ms-powerpoint' => 'ppt',
  122. 'application/vnd.ms-project' => 'mpp',
  123. 'application/vnd.ms-tnef' => 'base64',
  124. 'application/vnd.ms-works' => 'base64',
  125. 'application/vnd.ms-wpl' => 'wpl',
  126. 'application/vnd.mseq' => 'mseq',
  127. 'application/vnd.nervana' => 'ent',
  128. 'application/vnd.nokia.radio-preset' => 'rpst',
  129. 'application/vnd.nokia.radio-presets' => 'rpss',
  130. 'application/vnd.oasis.opendocument.text' => 'odt',
  131. 'application/vnd.oasis.opendocument.text-template' => 'ott',
  132. 'application/vnd.oasis.opendocument.text-web' => 'oth',
  133. 'application/vnd.oasis.opendocument.text-master' => 'odm',
  134. 'application/vnd.oasis.opendocument.graphics' => 'odg',
  135. 'application/vnd.oasis.opendocument.graphics-template' => 'otg',
  136. 'application/vnd.oasis.opendocument.presentation' => 'odp',
  137. 'application/vnd.oasis.opendocument.presentation-template' => 'otp',
  138. 'application/vnd.oasis.opendocument.spreadsheet' => 'ods',
  139. 'application/vnd.oasis.opendocument.spreadsheet-template' => 'ots',
  140. 'application/vnd.oasis.opendocument.chart' => 'odc',
  141. 'application/vnd.oasis.opendocument.formula' => 'odf',
  142. 'application/vnd.oasis.opendocument.database' => 'odb',
  143. 'application/vnd.oasis.opendocument.image' => 'odi',
  144. 'application/vnd.openxmlformats-officedocument.wordprocessingml.document' => 'docx',
  145. 'application/vnd.openxmlformats-officedocument.wordprocessingml.template' => 'dotx',
  146. 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' => 'xlsx',
  147. 'application/vnd.openxmlformats-officedocument.presentationml.presentation' => 'pptx',
  148. 'application/vnd.palm' => 'prc',
  149. 'application/vnd.picsel' => 'efif',
  150. 'application/vnd.pvi.ptid1' => 'pti',
  151. 'application/vnd.quark.quarkxpress' => 'qxd',
  152. 'application/vnd.sealed.doc' => 'sdoc',
  153. 'application/vnd.sealed.eml' => 'seml',
  154. 'application/vnd.sealed.mht' => 'smht',
  155. 'application/vnd.sealed.ppt' => 'sppt',
  156. 'application/vnd.sealed.xls' => 'sxls',
  157. 'application/vnd.sealedmedia.softseal.html' => 'stml',
  158. 'application/vnd.sealedmedia.softseal.pdf' => 'spdf',
  159. 'application/vnd.seemail' => 'see',
  160. 'application/vnd.smaf' => 'mmf',
  161. 'application/vnd.sun.xml.calc' => 'sxc',
  162. 'application/vnd.sun.xml.calc.template' => 'stc',
  163. 'application/vnd.sun.xml.draw' => 'sxd',
  164. 'application/vnd.sun.xml.draw.template' => 'std',
  165. 'application/vnd.sun.xml.impress' => 'sxi',
  166. 'application/vnd.sun.xml.impress.template' => 'sti',
  167. 'application/vnd.sun.xml.math' => 'sxm',
  168. 'application/vnd.sun.xml.writer' => 'sxw',
  169. 'application/vnd.sun.xml.writer.global' => 'sxg',
  170. 'application/vnd.sun.xml.writer.template' => 'stw',
  171. 'application/vnd.sus-calendar' => 'sus',
  172. 'application/vnd.vidsoft.vidconference' => 'vsc',
  173. 'application/vnd.visio' => 'vsd',
  174. 'application/vnd.visionary' => 'vis',
  175. 'application/vnd.wap.sic' => 'sic',
  176. 'application/vnd.wap.slc' => 'slc',
  177. 'application/vnd.wap.wbxml' => 'wbxml',
  178. 'application/vnd.wap.wmlc' => 'wmlc',
  179. 'application/vnd.wap.wmlscriptc' => 'wmlsc',
  180. 'application/vnd.webturbo' => 'wtb',
  181. 'application/vnd.wordperfect' => 'wpd',
  182. 'application/vnd.wqd' => 'wqd',
  183. 'application/vnd.wv.csp+wbxml' => 'wv',
  184. 'application/vnd.wv.csp+xml' => '8bit',
  185. 'application/vnd.wv.ssp+xml' => '8bit',
  186. 'application/vnd.yamaha.hv-dic' => 'hvd',
  187. 'application/vnd.yamaha.hv-script' => 'hvs',
  188. 'application/vnd.yamaha.hv-voice' => 'hvp',
  189. 'application/vnd.yamaha.smaf-audio' => 'saf',
  190. 'application/vnd.yamaha.smaf-phrase' => 'spf',
  191. 'application/vocaltec-media-desc' => 'vmd',
  192. 'application/vocaltec-media-file' => 'vmf',
  193. 'application/vocaltec-talker' => 'vtk',
  194. 'application/watcherinfo+xml' => 'wif',
  195. 'application/wordperfect5.1' => 'wp5',
  196. 'application/x-123' => 'wk',
  197. 'application/x-7th_level_event' => '7ls',
  198. 'application/x-authorware-bin' => 'aab',
  199. 'application/x-authorware-map' => 'aam',
  200. 'application/x-authorware-seg' => 'aas',
  201. 'application/x-bcpio' => 'bcpio',
  202. 'application/x-bleeper' => 'bleep',
  203. 'application/x-bzip2' => 'bz2',
  204. 'application/x-cdlink' => 'vcd',
  205. 'application/x-chat' => 'chat',
  206. 'application/x-chess-pgn' => 'pgn',
  207. 'application/x-compress' => 'z',
  208. 'application/x-cpio' => 'cpio',
  209. 'application/x-cprplayer' => 'pqf',
  210. 'application/x-csh' => 'csh',
  211. 'application/x-cu-seeme' => 'csm',
  212. 'application/x-cult3d-object' => 'co',
  213. 'application/x-debian-package' => 'deb',
  214. 'application/x-director' => 'dcr',
  215. 'application/x-dvi' => 'dvi',
  216. 'application/x-envoy' => 'evy',
  217. 'application/x-futuresplash' => 'spl',
  218. 'application/x-gtar' => 'gtar',
  219. 'application/x-gzip' => 'gz',
  220. 'application/x-hdf' => 'hdf',
  221. 'application/x-hep' => 'hep',
  222. 'application/x-html+ruby' => 'rhtml',
  223. 'application/x-httpd-miva' => 'mv',
  224. 'application/x-httpd-php' => 'phtml',
  225. 'application/x-ica' => 'ica',
  226. 'application/x-imagemap' => 'imagemap',
  227. 'application/x-ipix' => 'ipx',
  228. 'application/x-ipscript' => 'ips',
  229. 'application/x-java-archive' => 'jar',
  230. 'application/x-java-jnlp-file' => 'jnlp',
  231. 'application/x-java-serialized-object' => 'ser',
  232. 'application/x-java-vm' => 'class',
  233. 'application/x-javascript' => 'js',
  234. 'application/x-koan' => 'skp',
  235. 'application/x-latex' => 'latex',
  236. 'application/x-mac-compactpro' => 'cpt',
  237. 'application/x-maker' => 'frm',
  238. 'application/x-mathcad' => 'mcd',
  239. 'application/x-midi' => 'mid',
  240. 'application/x-mif' => 'mif',
  241. 'application/x-msaccess' => 'mda',
  242. 'application/x-msdos-program' => 'com',
  243. 'application/x-msdownload' => 'base64',
  244. 'application/x-msexcel' => 'xls',
  245. 'application/x-msword' => 'doc',
  246. 'application/x-netcdf' => 'nc',
  247. 'application/x-ns-proxy-autoconfig' => 'pac',
  248. 'application/x-pagemaker' => 'pm5',
  249. 'application/x-perl' => 'pl',
  250. 'application/x-pn-realmedia' => 'rp',
  251. 'application/x-python' => 'py',
  252. 'application/x-quicktimeplayer' => 'qtl',
  253. 'application/x-rar-compressed' => 'rar',
  254. 'application/x-ruby' => 'rb',
  255. 'application/x-sh' => 'sh',
  256. 'application/x-shar' => 'shar',
  257. 'application/x-shockwave-flash' => 'swf',
  258. 'application/x-sprite' => 'spr',
  259. 'application/x-spss' => 'sav',
  260. 'application/x-spt' => 'spt',
  261. 'application/x-stuffit' => 'sit',
  262. 'application/x-sv4cpio' => 'sv4cpio',
  263. 'application/x-sv4crc' => 'sv4crc',
  264. 'application/x-tar' => 'tar',
  265. 'application/x-tcl' => 'tcl',
  266. 'application/x-tex' => 'tex',
  267. 'application/x-texinfo' => 'texinfo',
  268. 'application/x-troff' => 't',
  269. 'application/x-troff-man' => 'man',
  270. 'application/x-troff-me' => 'me',
  271. 'application/x-troff-ms' => 'ms',
  272. 'application/x-twinvq' => 'vqf',
  273. 'application/x-twinvq-plugin' => 'vqe',
  274. 'application/x-ustar' => 'ustar',
  275. 'application/x-vmsbackup' => 'bck',
  276. 'application/x-wais-source' => 'src',
  277. 'application/x-wingz' => 'wz',
  278. 'application/x-word' => 'base64',
  279. 'application/x-wordperfect6.1' => 'wp6',
  280. 'application/x-x509-ca-cert' => 'crt',
  281. 'application/x-zip-compressed' => 'zip',
  282. 'application/xhtml+xml' => 'xhtml',
  283. 'application/zip' => 'zip',
  284. 'audio/3gpp' => '3gpp',
  285. 'audio/amr' => 'amr',
  286. 'audio/amr-wb' => 'awb',
  287. 'audio/basic' => 'au',
  288. 'audio/evrc' => 'evc',
  289. 'audio/l16' => 'l16',
  290. 'audio/midi' => 'mid',
  291. 'audio/mpeg' => 'mp3',
  292. 'audio/prs.sid' => 'sid',
  293. 'audio/qcelp' => 'qcp',
  294. 'audio/smv' => 'smv',
  295. 'audio/vnd.audiokoz' => 'koz',
  296. 'audio/vnd.digital-winds' => 'eol',
  297. 'audio/vnd.everad.plj' => 'plj',
  298. 'audio/vnd.lucent.voice' => 'lvp',
  299. 'audio/vnd.nokia.mobile-xmf' => 'mxmf',
  300. 'audio/vnd.nortel.vbk' => 'vbk',
  301. 'audio/vnd.nuera.ecelp4800' => 'ecelp4800',
  302. 'audio/vnd.nuera.ecelp7470' => 'ecelp7470',
  303. 'audio/vnd.nuera.ecelp9600' => 'ecelp9600',
  304. 'audio/vnd.sealedmedia.softseal.mpeg' => 'smp3',
  305. 'audio/voxware' => 'vox',
  306. 'audio/x-aiff' => 'aif',
  307. 'audio/x-mid' => 'mid',
  308. 'audio/x-midi' => 'mid',
  309. 'audio/x-mpeg' => 'mp2',
  310. 'audio/x-mpegurl' => 'mpu',
  311. 'audio/x-pn-realaudio' => 'rm',
  312. 'audio/x-pn-realaudio-plugin' => 'rpm',
  313. 'audio/x-realaudio' => 'ra',
  314. 'audio/x-wav' => 'wav',
  315. 'chemical/x-csml' => 'csm',
  316. 'chemical/x-embl-dl-nucleotide' => 'emb',
  317. 'chemical/x-gaussian-cube' => 'cube',
  318. 'chemical/x-gaussian-input' => 'gau',
  319. 'chemical/x-jcamp-dx' => 'jdx',
  320. 'chemical/x-mdl-molfile' => 'mol',
  321. 'chemical/x-mdl-rxnfile' => 'rxn',
  322. 'chemical/x-mdl-tgf' => 'tgf',
  323. 'chemical/x-mopac-input' => 'mop',
  324. 'chemical/x-pdb' => 'pdb',
  325. 'chemical/x-rasmol' => 'scr',
  326. 'chemical/x-xyz' => 'xyz',
  327. 'drawing/dwf' => 'dwf',
  328. 'drawing/x-dwf' => 'dwf',
  329. 'i-world/i-vrml' => 'ivr',
  330. 'image/bmp' => 'bmp',
  331. 'image/cewavelet' => 'wif',
  332. 'image/cis-cod' => 'cod',
  333. 'image/fif' => 'fif',
  334. 'image/gif' => 'gif',
  335. 'image/ief' => 'ief',
  336. 'image/jp2' => 'jp2',
  337. 'image/jpeg' => 'jpg',
  338. 'image/jpm' => 'jpm',
  339. 'image/jpx' => 'jpf',
  340. 'image/pict' => 'pic',
  341. 'image/pjpeg' => 'jpg',
  342. 'image/png' => 'png',
  343. 'image/targa' => 'tga',
  344. 'image/tiff' => 'tif',
  345. 'image/vn-svf' => 'svf',
  346. 'image/vnd.dgn' => 'dgn',
  347. 'image/vnd.djvu' => 'djvu',
  348. 'image/vnd.dwg' => 'dwg',
  349. 'image/vnd.glocalgraphics.pgb' => 'pgb',
  350. 'image/vnd.microsoft.icon' => 'ico',
  351. 'image/vnd.ms-modi' => 'mdi',
  352. 'image/vnd.sealed.png' => 'spng',
  353. 'image/vnd.sealedmedia.softseal.gif' => 'sgif',
  354. 'image/vnd.sealedmedia.softseal.jpg' => 'sjpg',
  355. 'image/vnd.wap.wbmp' => 'wbmp',
  356. 'image/x-bmp' => 'bmp',
  357. 'image/x-cmu-raster' => 'ras',
  358. 'image/x-freehand' => 'fh4',
  359. 'image/x-png' => 'png',
  360. 'image/x-portable-anymap' => 'pnm',
  361. 'image/x-portable-bitmap' => 'pbm',
  362. 'image/x-portable-graymap' => 'pgm',
  363. 'image/x-portable-pixmap' => 'ppm',
  364. 'image/x-rgb' => 'rgb',
  365. 'image/x-xbitmap' => 'xbm',
  366. 'image/x-xpixmap' => 'xpm',
  367. 'image/x-xwindowdump' => 'xwd',
  368. 'message/external-body' => '8bit',
  369. 'message/news' => '8bit',
  370. 'message/partial' => '8bit',
  371. 'message/rfc822' => '8bit',
  372. 'model/iges' => 'igs',
  373. 'model/mesh' => 'msh',
  374. 'model/vnd.parasolid.transmit.binary' => 'x_b',
  375. 'model/vnd.parasolid.transmit.text' => 'x_t',
  376. 'model/vrml' => 'wrl',
  377. 'multipart/alternative' => '8bit',
  378. 'multipart/appledouble' => '8bit',
  379. 'multipart/digest' => '8bit',
  380. 'multipart/mixed' => '8bit',
  381. 'multipart/parallel' => '8bit',
  382. 'text/comma-separated-values' => 'csv',
  383. 'text/css' => 'css',
  384. 'text/html' => 'html',
  385. 'text/plain' => 'txt',
  386. 'text/prs.fallenstein.rst' => 'rst',
  387. 'text/richtext' => 'rtx',
  388. 'text/rtf' => 'rtf',
  389. 'text/sgml' => 'sgml',
  390. 'text/tab-separated-values' => 'tsv',
  391. 'text/vnd.net2phone.commcenter.command' => 'ccc',
  392. 'text/vnd.sun.j2me.app-descriptor' => 'jad',
  393. 'text/vnd.wap.si' => 'si',
  394. 'text/vnd.wap.sl' => 'sl',
  395. 'text/vnd.wap.wml' => 'wml',
  396. 'text/vnd.wap.wmlscript' => 'wmls',
  397. 'text/x-hdml' => 'hdml',
  398. 'text/x-setext' => 'etx',
  399. 'text/x-sgml' => 'sgml',
  400. 'text/x-speech' => 'talk',
  401. 'text/x-vcalendar' => 'vcs',
  402. 'text/x-vcard' => 'vcf',
  403. 'text/xml' => 'xml',
  404. 'ulead/vrml' => 'uvr',
  405. 'video/3gpp' => '3gp',
  406. 'video/dl' => 'dl',
  407. 'video/gl' => 'gl',
  408. 'video/mj2' => 'mj2',
  409. 'video/mpeg' => 'mpeg',
  410. 'video/quicktime' => 'mov',
  411. 'video/vdo' => 'vdo',
  412. 'video/vivo' => 'viv',
  413. 'video/vnd.fvt' => 'fvt',
  414. 'video/vnd.mpegurl' => 'mxu',
  415. 'video/vnd.nokia.interleaved-multimedia' => 'nim',
  416. 'video/vnd.objectvideo' => 'mp4',
  417. 'video/vnd.sealed.mpeg1' => 's11',
  418. 'video/vnd.sealed.mpeg4' => 'smpg',
  419. 'video/vnd.sealed.swf' => 'sswf',
  420. 'video/vnd.sealedmedia.softseal.mov' => 'smov',
  421. 'video/vnd.vivo' => 'vivo',
  422. 'video/x-fli' => 'fli',
  423. 'video/x-ms-asf' => 'asf',
  424. 'video/x-ms-wmv' => 'wmv',
  425. 'video/x-msvideo' => 'avi',
  426. 'video/x-sgi-movie' => 'movie',
  427. 'x-chemical/x-pdb' => 'pdb',
  428. 'x-chemical/x-xyz' => 'xyz',
  429. 'x-conference/x-cooltalk' => 'ice',
  430. 'x-drawing/dwf' => 'dwf',
  431. 'x-world/x-d96' => 'd',
  432. 'x-world/x-svr' => 'svr',
  433. 'x-world/x-vream' => 'vrw',
  434. 'x-world/x-vrml' => 'wrl',
  435. );
  436. /**
  437. * Stores the absolute path to the document root directory
  438. * @var string
  439. */
  440. static protected $documentRoot;
  441. /**
  442. * The absolute path to the file without dots
  443. * @var string
  444. */
  445. protected $path;
  446. /**
  447. * Sets the path t the document root directory
  448. *
  449. * @param string $documentRoot
  450. */
  451. static public function setDocumentRoot($documentRoot)
  452. {
  453. if (!is_dir($documentRoot)) {
  454. throw new \LogicException($documentRoot . ' is no directory');
  455. }
  456. self::$documentRoot = realpath($documentRoot);
  457. }
  458. /**
  459. * Returns the path to the document root directory
  460. *
  461. * @return string
  462. */
  463. static public function getDocumentRoot()
  464. {
  465. return self::$documentRoot;
  466. }
  467. /**
  468. * Constructs a new file from the given path.
  469. *
  470. * @param string $path The path to the file
  471. * @throws FileNotFoundException If the given path is no file
  472. */
  473. public function __construct($path)
  474. {
  475. if (!is_file($path)) {
  476. throw new FileNotFoundException($path);
  477. }
  478. $this->path = realpath($path);
  479. }
  480. /**
  481. * Alias for getPath()
  482. *
  483. * @return string
  484. */
  485. public function __toString()
  486. {
  487. return null === $this->getPath() ? '' : $this->getPath();
  488. }
  489. /**
  490. * Returns the file name
  491. *
  492. * @return string
  493. */
  494. public function getName()
  495. {
  496. return basename($this->path);
  497. }
  498. /**
  499. * Returns the file extension (with dot)
  500. *
  501. * @return string
  502. */
  503. public function getExtension()
  504. {
  505. if ($ext = pathinfo($this->getName(), \PATHINFO_EXTENSION)) {
  506. return '.' . $ext;
  507. } else {
  508. return '';
  509. }
  510. }
  511. /**
  512. * Returns the extension based on the mime type (with dot)
  513. *
  514. * If the mime type is unknown, the actual extension is returned instead.
  515. *
  516. * @return string
  517. */
  518. public function getDefaultExtension()
  519. {
  520. $type = $this->getMimeType();
  521. if (isset(self::$defaultExtensions[$type])) {
  522. return '.' . self::$defaultExtensions[$type];
  523. } else {
  524. return $this->getExtension();
  525. }
  526. }
  527. /**
  528. * Returns the directory of the file
  529. *
  530. * @return string
  531. */
  532. public function getDirectory()
  533. {
  534. return dirname($this->path);
  535. }
  536. /**
  537. * Returns the absolute file path without dots
  538. *
  539. * @returns string The file path
  540. */
  541. public function getPath()
  542. {
  543. return $this->path;
  544. }
  545. /**
  546. * Returns the path relative to the document root
  547. *
  548. * You can set the document root using the static method setDocumentRoot().
  549. * If the file is outside of the document root, this method returns an
  550. * empty string.
  551. *
  552. * @return string The relative file path
  553. */
  554. public function getWebPath()
  555. {
  556. $root = self::$documentRoot;
  557. if (strpos($this->path, $root) === false) {
  558. return '';
  559. }
  560. return str_replace(array($root, DIRECTORY_SEPARATOR), array('', '/'), $this->path);
  561. }
  562. /**
  563. * Returns the mime type of the file.
  564. *
  565. * The mime type is guessed using the functions finfo(), mime_content_type()
  566. * and the system binary "file" (in this order), depending on which of those
  567. * is available on the current operating system.
  568. *
  569. * @returns string The guessed mime type, e.g. "application/pdf"
  570. */
  571. public function getMimeType()
  572. {
  573. $guesser = MimeTypeGuesser::getInstance();
  574. return $guesser->guess($this->getPath());
  575. }
  576. /**
  577. * Returns the size of this file
  578. *
  579. * @return integer The file size in bytes
  580. */
  581. public function size()
  582. {
  583. if (false === ($size = filesize($this->getPath()))) {
  584. throw new FileException(sprintf('Could not read file size of %s', $this->getPath()));
  585. }
  586. return $size;
  587. }
  588. /**
  589. * Moves the file to a new directory and gives it a new filename
  590. *
  591. * @param string $directory The new directory
  592. * @param string $filename The new file name
  593. * @throws FileException When the file could not be moved
  594. */
  595. protected function doMove($directory, $filename)
  596. {
  597. $newPath = $directory . DIRECTORY_SEPARATOR . $filename;
  598. if (!rename($this->getPath(), $newPath)) {
  599. throw new FileException(sprintf('Could not move file %s to %s', $this->getPath(), $newPath));
  600. }
  601. $this->path = realpath($newPath);
  602. }
  603. /**
  604. * Moves the file to a new location.
  605. *
  606. * @param string $directory The destination folder
  607. * @param string $name The new file name
  608. */
  609. public function move($directory, $name = null)
  610. {
  611. $this->doMove($directory, $this->getName());
  612. if (null !== $name) {
  613. $this->rename($name);
  614. }
  615. }
  616. /**
  617. * Renames the file
  618. *
  619. * @param string $name The new file name
  620. */
  621. public function rename($name)
  622. {
  623. $this->doMove($this->getDirectory(), $name);
  624. }
  625. }