Mime.php 916 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  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. declare(strict_types=1);
  10. namespace PhpMyAdmin;
  11. /**
  12. * PhpMyAdmin\Mime class;
  13. *
  14. * @package PhpMyAdmin
  15. */
  16. class Mime
  17. {
  18. /**
  19. * Tries to detect MIME type of content.
  20. *
  21. * @param string $test First few bytes of content to use for detection
  22. *
  23. * @return string
  24. */
  25. public static function detect(&$test)
  26. {
  27. $len = mb_strlen($test);
  28. if ($len >= 2 && $test[0] == chr(0xff) && $test[1] == chr(0xd8)) {
  29. return 'image/jpeg';
  30. }
  31. if ($len >= 3 && substr($test, 0, 3) == 'GIF') {
  32. return 'image/gif';
  33. }
  34. if ($len >= 4 && mb_substr($test, 0, 4) == "\x89PNG") {
  35. return 'image/png';
  36. }
  37. return 'application/octet-stream';
  38. }
  39. }