Skip to content

Commit 9e4b3ac

Browse files
[HttpFoundation] fix guessing mime-types of files with leading dash
1 parent a5d46a3 commit 9e4b3ac

File tree

3 files changed

+12
-3
lines changed

3 files changed

+12
-3
lines changed

File/MimeType/FileBinaryMimeTypeGuesser.php

+2-2
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

+10-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,16 @@
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+
} finally {
30+
chdir($cwd);
31+
}
32+
}
2433

2534
public function testGuessImageWithoutExtension()
2635
{

0 commit comments

Comments
 (0)