diff --git a/FileBinaryMimeTypeGuesser.php b/FileBinaryMimeTypeGuesser.php index 1faa811..fe1e0cd 100644 --- a/FileBinaryMimeTypeGuesser.php +++ b/FileBinaryMimeTypeGuesser.php @@ -31,7 +31,7 @@ class FileBinaryMimeTypeGuesser implements MimeTypeGuesserInterface * * @param string $cmd The command to run to get the MIME type of a file */ - public function __construct(string $cmd = 'file -b --mime %s 2>/dev/null') + public function __construct(string $cmd = 'file -b --mime -- %s 2>/dev/null') { $this->cmd = $cmd; } @@ -74,7 +74,7 @@ public function guessMimeType(string $path): ?string ob_start(); // need to use --mime instead of -i. see #6641 - passthru(sprintf($this->cmd, escapeshellarg($path)), $return); + passthru(sprintf($this->cmd, escapeshellarg((0 === strpos($path, '-') ? './' : '').$path)), $return); if ($return > 0) { ob_end_clean(); diff --git a/Tests/AbstractMimeTypeGuesserTest.php b/Tests/AbstractMimeTypeGuesserTest.php index 3ac9382..70e419c 100644 --- a/Tests/AbstractMimeTypeGuesserTest.php +++ b/Tests/AbstractMimeTypeGuesserTest.php @@ -27,6 +27,21 @@ public static function tearDownAfterClass(): void abstract protected function getGuesser(): MimeTypeGuesserInterface; + public function testGuessWithLeadingDash() + { + if (!$this->getGuesser()->isGuesserSupported()) { + $this->markTestSkipped('Guesser is not supported'); + } + + $cwd = getcwd(); + chdir(__DIR__.'/Fixtures/mimetypes'); + try { + $this->assertEquals('image/gif', $this->getGuesser()->guessMimeType('-test')); + } finally { + chdir($cwd); + } + } + public function testGuessImageWithoutExtension() { if (!$this->getGuesser()->isGuesserSupported()) { diff --git a/Tests/Fixtures/mimetypes/-test b/Tests/Fixtures/mimetypes/-test new file mode 100644 index 0000000..b636f4b Binary files /dev/null and b/Tests/Fixtures/mimetypes/-test differ