8000 Keep the file extension in the temporary copy and test that it exists… · symfony/symfony@4cf06c1 · GitHub
[go: up one dir, main page]

Skip to content

Commit 4cf06c1

Browse files
thewilkybarkidfabpot
authored andcommitted
Keep the file extension in the temporary copy and test that it exists (closes #7482)
1 parent b898b13 commit 4cf06c1

File tree

3 files changed

+28
-3
lines changed

3 files changed

+28
-3
lines changed

src/Symfony/Component/DomCrawler/Field/FileFormField.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,17 @@ public function setValue($value)
5959
if (null !== $value && is_readable($value)) {
6060
$error = UPLOAD_ERR_OK;
6161
$size = filesize($value);
62-
$name = basename($value);
62+
$info = pathinfo($value);
63+
$name = $info['basename'];
6364

6465
// copy to a tmp location
65-
$tmp = tempnam(sys_get_temp_dir(), 'upload');
66-
unlink($tmp);
66+
$tmp = sys_get_temp_dir().'/'.sha1(uniqid(mt_rand(), true));
67+
if (array_key_exists('extension', $info)) {
68+
$tmp .= '.'.$info['extension'];
69+
}
70+
if (is_file($tmp)) {
71+
unlink($tmp);
72+
}
6773
copy($value, $tmp);
6874
$value = $tmp;
6975
} else {

src/Symfony/Component/DomCrawler/Tests/Field/FileFormFieldTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,26 @@ public function testSetValue($method)
5656
$this->assertEquals(basename(__FILE__), $value['name'], "->$method() sets the name of the file field");
5757
$this->assertEquals('', $value['type'], "->$method() sets the type of the file field");
5858
$this->assertInternalType('string', $value['tmp_name'], "->$method() sets the tmp_name of the file field");
59+
$this->assertFileExists($value['tmp_name'], "->$method() creates a copy of the file at the tmp_name path");
5960
$this->assertEquals(0, $value['error'], "->$method() sets the error of the file field");
6061
$this->assertEquals(filesize(__FILE__), $value['size'], "->$method() sets the size of the file field");
62+
63+
$origInfo = pathinfo(__FILE__);
64+
$tmpInfo = pathinfo($value['tmp_name']);
65+
$this->assertEquals(
66+
$origInfo['extension'],
67+
$tmpInfo['extension'],
68+
"->$method() keeps the same file extension in the tmp_name copy"
69+
);
70+
71+
$field->$method(__DIR__.'/../Fixtures/no-extension');
72+
$value = $field->getValue();
73+
74+
$this->assertArrayNotHasKey(
75+
'extension',
76+
pathinfo($value['tmp_name']),
77+
"->$method() does not add a file extension in the tmp_name copy"
78+
);
6179
}
6280

6381
public function getSetValueMethods()
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Test

0 commit comments

Comments
 (0)
0