Skip to content
This repository was archived by the owner on Jan 30, 2020. It is now read-only.

Commit 47e9599

Browse files
committedJan 25, 2016
[BUG] This commit allows the upload of same filenames in input type file
For example, Ios Safari gives the name image.jpg for pcitures from the gallery In the present situation, the first files would be overriden by the last file with the same name. And the getValue of these inputs will return the same fileName with no really mean to identify it. With the use of the unique name from PHP $_FILES in the name column of the $_files array of the Http adapter, the files will be unique and easily identifiable. - Fix the unit tests and add a new one for the same filename case.
1 parent 8c9e54a commit 47e9599

File tree

8 files changed

+57
-12
lines changed

8 files changed

+57
-12
lines changed
 

‎src/Transfer/Adapter/Http.php

+6
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,9 @@ protected function prepareFiles()
423423

424424
$this->files[$form]['name'] = $form;
425425
foreach ($this->files[$form]['multifiles'] as $key => $value) {
426+
if ($this->files[$value]['tmp_name'] !== '') {
427+
$this->files[$value]['name'] = basename($this->files[$value]['tmp_name']) . '_' . $this->files[$value]['name'];
428+
}
426429
$this->files[$value]['options'] = $this->options;
427430
$this->files[$value]['validated'] = false;
428431
$this->files[$value]['received'] = false;
@@ -441,6 +444,9 @@ protected function prepareFiles()
441444
}
442445
} else {
443446
$this->files[$form] = $content;
447+
if ($this->files[$form]['tmp_name'] !== '') {
448+
$this->files[$form]['name'] = basename($this->files[$form]['tmp_name']) . '_' . $this->files[$form]['name'];
449+
}
444450
$this->files[$form]['options'] = $this->options;
445451
$this->files[$form]['validated'] = false;
446452
$this->files[$form]['received'] = false;

‎test/Transfer/Adapter/HttpTest.php

+47-11
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@ public function setUp()
3131
{
3232
$_FILES = [
3333
'txt' => [
34-
'name' => __DIR__ . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'test.txt',
34+
'name' => 'test.txt',
3535
'type' => 'plain/text',
3636
'size' => 8,
37-
'tmp_name' => __DIR__ . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'test.txt',
37+
'tmp_name' => __DIR__ . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'php0zgByO',
3838
'error' => 0]];
3939
$this->adapter = new HttpTestMockAdapter();
4040
}
@@ -52,7 +52,7 @@ public function tearDown()
5252
public function testEmptyAdapter()
5353
{
5454
$files = $this->adapter->getFileName();
55-
$this->assertContains('test.txt', $files);
55+
$this->assertContains('php0zgByO_test.txt', $files);
5656
}
5757

5858
public function testAutoSetUploadValidator()
@@ -147,33 +147,69 @@ public function testMultiFiles()
147147
{
148148
$_FILES = [
149149
'txt' => [
150-
'name' => __DIR__ . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'test.txt',
150+
'name' => 'test.txt',
151151
'type' => 'plain/text',
152152
'size' => 8,
153-
'tmp_name' => __DIR__ . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'test.txt',
153+
'tmp_name' => __DIR__ . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'php0zgByO',
154154
'error' => 0],
155155
'exe' => [
156156
'name' => [
157-
0 => __DIR__ . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'file1.txt',
158-
1 => __DIR__ . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'file2.txt'],
157+
0 => 'file1.exe',
158+
1 => 'file2.exe'],
159159
'type' => [
160160
0 => 'plain/text',
161161
1 => 'plain/text'],
162162
'size' => [
163163
0 => 8,
164164
1 => 8],
165165
'tmp_name' => [
166-
0 => __DIR__ . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'file1.txt',
167-
1 => __DIR__ . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'file2.txt'],
166+
0 => __DIR__ . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'phpqBXGTg',
167+
1 => __DIR__ . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'phpZqRDQF'],
168168
'error' => [
169169
0 => 0,
170170
1 => 0]]];
171171
$adapter = new HttpTestMockAdapter();
172172
$adapter->setOptions(['ignoreNoFile' => true]);
173173
$this->assertTrue($adapter->receive('exe'));
174174
$this->assertEquals(
175-
['exe_0_' => __DIR__ . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'file1.txt',
176-
'exe_1_' => __DIR__ . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'file2.txt'],
175+
['exe_0_' => 'phpqBXGTg_file1.exe',
176+
'exe_1_' => 'phpZqRDQF_file2.exe'],
177+
$adapter->getFileName('exe', false));
178+
}
179+
180+
181+
public function testMultiFilesSameName()
182+
{
183+
$_FILES = [
184+
'txt' => [
185+
'name' => 'test.txt',
186+
'type' => 'plain/text',
187+
'size' => 8,
188+
'tmp_name' => dirname(__FILE__) . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'php0zgByO',
189+
'error' => 0
190+
],
191+
'exe' => [
192+
'name' => [
193+
0 => 'file.exe',
194+
1 => 'file.exe'],
195+
'type' => [
196+
0 => 'plain/text',
197+
1 => 'plain/text'],
198+
'size' => [
199+
0 => 8,
200+
1 => 8],
201+
'tmp_name' => [
202+
0 => dirname(__FILE__) . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'phpOOwDDc',
203+
1 => dirname(__FILE__) . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'phpDlIxkx'],
204+
'error' => [
205+
0 => 0,
206+
1 => 0]]];
207+
$adapter = new HttpTestMockAdapter();
208+
$adapter->setOptions(['ignoreNoFile' => true]);
209+
$this->assertTrue($adapter->receive('exe'));
210+
$this->assertEquals(
211+
['exe_0_' => 'phpOOwDDc_file.exe',
212+
'exe_1_' => 'phpDlIxkx_file.exe'],
177213
$adapter->getFileName('exe', false));
178214
}
179215

‎test/Transfer/Adapter/_files/file2.txt

-1
This file was deleted.
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
testfile
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
testfile
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
testfile
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
testfile

0 commit comments

Comments
 (0)
This repository has been archived.