demo.mimeonly.php 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <?php
  2. /////////////////////////////////////////////////////////////////
  3. /// getID3() by James Heinrich <info@getid3.org> //
  4. // available at https://github.com/JamesHeinrich/getID3 //
  5. // or https://www.getid3.org //
  6. // or http://getid3.sourceforge.net //
  7. // //
  8. // /demo/demo.mimeonly.php - part of getID3() //
  9. // Sample script for scanning a single file and returning only //
  10. // the MIME information //
  11. // see readme.txt for more details //
  12. // ///
  13. /////////////////////////////////////////////////////////////////
  14. die('For security reasons, this demo has been disabled. It can be enabled by removing line '.__LINE__.' in demos/'.basename(__FILE__));
  15. echo '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">';
  16. echo '<html><head><title>getID3 demos - MIME type only</title><style type="text/css">BODY, TD, TH { font-family: sans-serif; font-size: 10pt; }</style></head><body>';
  17. if (!empty($_REQUEST['filename'])) {
  18. echo 'The file "'.htmlentities($_REQUEST['filename']).'" has a MIME type of "'.htmlentities(GetMIMEtype($_REQUEST['filename'])).'"';
  19. } else {
  20. echo 'Usage: <span style="font-family: monospace;">'.htmlentities($_SERVER['PHP_SELF']).'?filename=<i>filename.ext</i></span>';
  21. }
  22. function GetMIMEtype($filename) {
  23. $filename = realpath($filename);
  24. if (!file_exists($filename)) {
  25. echo 'File does not exist: "'.htmlentities($filename).'"<br>';
  26. return '';
  27. } elseif (!is_readable($filename)) {
  28. echo 'File is not readable: "'.htmlentities($filename).'"<br>';
  29. return '';
  30. }
  31. // include getID3() library (can be in a different directory if full path is specified)
  32. require_once('../getid3/getid3.php');
  33. // Initialize getID3 engine
  34. $getID3 = new getID3;
  35. $DeterminedMIMEtype = '';
  36. if ($fp = fopen($filename, 'rb')) {
  37. $getID3->openfile($filename);
  38. if (empty($getID3->info['error'])) {
  39. // ID3v2 is the only tag format that might be prepended in front of files, and it's non-trivial to skip, easier just to parse it and know where to skip to
  40. getid3_lib::IncludeDependency(GETID3_INCLUDEPATH.'module.tag.id3v2.php', __FILE__, true);
  41. $getid3_id3v2 = new getid3_id3v2($getID3);
  42. $getid3_id3v2->Analyze();
  43. fseek($fp, $getID3->info['avdataoffset'], SEEK_SET);
  44. $formattest = fread($fp, 16); // 16 bytes is sufficient for any format except ISO CD-image
  45. fclose($fp);
  46. $DeterminedFormatInfo = $getID3->GetFileFormat($formattest);
  47. $DeterminedMIMEtype = $DeterminedFormatInfo['mime_type'];
  48. } else {
  49. echo 'Failed to getID3->openfile "'.htmlentities($filename).'"<br>';
  50. }
  51. } else {
  52. echo 'Failed to fopen "'.htmlentities($filename).'"<br>';
  53. }
  54. return $DeterminedMIMEtype;
  55. }
  56. echo '</body></html>';