Skip to content

Commit 3929d9f

Browse files
[HttpFoundation] fix guessing mime-types of files with leading dash
1 parent 746f8d3 commit 3929d9f

File tree

3 files changed

+14
-3
lines changed

3 files changed

+14
-3
lines changed

File/MimeType/FileBinaryMimeTypeGuesser.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class FileBinaryMimeTypeGuesser implements MimeTypeGuesserInterface
3131
*
3232
* @param string $cmd The command to run to get the mime type of a file
3333
*/
34-
public function __construct($cmd = 'file -b --mime %s 2>/dev/null')
34+
public function __construct($cmd = 'file -b --mime -- %s 2>/dev/null')
3535
{
3636
$this->cmd = $cmd;
3737
}
@@ -80,7 +80,7 @@ public function guess($path)
8080
ob_start();
8181

8282
// need to use --mime instead of -i. see #6641
83-
passthru(sprintf($this->cmd, escapeshellarg($path)), $return);
83+
passthru(sprintf($this->cmd, escapeshellarg((0 === strpos($path, '-') ? './' : '').$path)), $return);
8484
if ($return > 0) {
8585
ob_end_clean();
8686

Tests/File/Fixtures/-test

35 Bytes
Binary file not shown.

Tests/File/MimeType/MimeTypeTest.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,18 @@
2020
*/
2121
class MimeTypeTest extends TestCase
2222
{
23-
protected $path;
23+
public function testGuessWithLeadingDash()
24+
{
25+
$cwd = getcwd();
26+
chdir(__DIR__.'/../Fixtures');
27+
try {
28+
$this->assertEquals('image/gif', MimeTypeGuesser::getInstance()->guess('-test'));
29+
chdir($cwd);
30+
} catch(\Exception $e) {
31+
chdir($cwd);
32+
throw $e;
33+
}
34+
}
2435

2536
public function testGuessImageWithoutExtension()
2637
{

0 commit comments

Comments
 (0)