Mime.php 891 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * MIME detection code.
  5. *
  6. * @package PhpMyAdmin
  7. * @todo Maybe we could try to use fileinfo module if loaded
  8. */
  9. namespace PhpMyAdmin;
  10. /**
  11. * PhpMyAdmin\Mime class;
  12. *
  13. * @package PhpMyAdmin
  14. */
  15. class Mime
  16. {
  17. /**
  18. * Tries to detect MIME type of content.
  19. *
  20. * @param string &$test First few bytes of content to use for detection
  21. *
  22. * @return string
  23. */
  24. public static function detect(&$test)
  25. {
  26. $len = mb_strlen($test);
  27. if ($len >= 2 && $test[0] == chr(0xff) && $test[1] == chr(0xd8)) {
  28. return 'image/jpeg';
  29. }
  30. if ($len >= 3 && substr($test, 0, 3) == 'GIF') {
  31. return 'image/gif';
  32. }
  33. if ($len >= 4 && mb_substr($test, 0, 4) == "\x89PNG") {
  34. return 'image/png';
  35. }
  36. return 'application/octet-stream';
  37. }
  38. }