File.php 21 KB

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