From 47e95990a734bc06b5d65fecc7bb184d152d606b Mon Sep 17 00:00:00 2001 From: codisart Date: Mon, 25 Jan 2016 12:41:30 +0100 Subject: [PATCH] [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. --- src/Transfer/Adapter/Http.php | 6 ++ test/Transfer/Adapter/HttpTest.php | 58 +++++++++++++++---- test/Transfer/Adapter/_files/file2.txt | 1 - .../Adapter/_files/{file1.txt => php0zgByO} | 0 test/Transfer/Adapter/_files/phpDlIxkx | 1 + test/Transfer/Adapter/_files/phpOOwDDc | 1 + test/Transfer/Adapter/_files/phpZqRDQF | 1 + test/Transfer/Adapter/_files/phpqBXGTg | 1 + 8 files changed, 57 insertions(+), 12 deletions(-) delete mode 100644 test/Transfer/Adapter/_files/file2.txt rename test/Transfer/Adapter/_files/{file1.txt => php0zgByO} (100%) create mode 100644 test/Transfer/Adapter/_files/phpDlIxkx create mode 100644 test/Transfer/Adapter/_files/phpOOwDDc create mode 100644 test/Transfer/Adapter/_files/phpZqRDQF create mode 100644 test/Transfer/Adapter/_files/phpqBXGTg diff --git a/src/Transfer/Adapter/Http.php b/src/Transfer/Adapter/Http.php index c6abfa9..d0136ed 100644 --- a/src/Transfer/Adapter/Http.php +++ b/src/Transfer/Adapter/Http.php @@ -423,6 +423,9 @@ protected function prepareFiles() $this->files[$form]['name'] = $form; foreach ($this->files[$form]['multifiles'] as $key => $value) { + if ($this->files[$value]['tmp_name'] !== '') { + $this->files[$value]['name'] = basename($this->files[$value]['tmp_name']) . '_' . $this->files[$value]['name']; + } $this->files[$value]['options'] = $this->options; $this->files[$value]['validated'] = false; $this->files[$value]['received'] = false; @@ -441,6 +444,9 @@ protected function prepareFiles() } } else { $this->files[$form] = $content; + if ($this->files[$form]['tmp_name'] !== '') { + $this->files[$form]['name'] = basename($this->files[$form]['tmp_name']) . '_' . $this->files[$form]['name']; + } $this->files[$form]['options'] = $this->options; $this->files[$form]['validated'] = false; $this->files[$form]['received'] = false; diff --git a/test/Transfer/Adapter/HttpTest.php b/test/Transfer/Adapter/HttpTest.php index fea4a13..fd51466 100644 --- a/test/Transfer/Adapter/HttpTest.php +++ b/test/Transfer/Adapter/HttpTest.php @@ -31,10 +31,10 @@ public function setUp() { $_FILES = [ 'txt' => [ - 'name' => __DIR__ . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'test.txt', + 'name' => 'test.txt', 'type' => 'plain/text', 'size' => 8, - 'tmp_name' => __DIR__ . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'test.txt', + 'tmp_name' => __DIR__ . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'php0zgByO', 'error' => 0]]; $this->adapter = new HttpTestMockAdapter(); } @@ -52,7 +52,7 @@ public function tearDown() public function testEmptyAdapter() { $files = $this->adapter->getFileName(); - $this->assertContains('test.txt', $files); + $this->assertContains('php0zgByO_test.txt', $files); } public function testAutoSetUploadValidator() @@ -147,15 +147,15 @@ public function testMultiFiles() { $_FILES = [ 'txt' => [ - 'name' => __DIR__ . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'test.txt', + 'name' => 'test.txt', 'type' => 'plain/text', 'size' => 8, - 'tmp_name' => __DIR__ . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'test.txt', + 'tmp_name' => __DIR__ . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'php0zgByO', 'error' => 0], 'exe' => [ 'name' => [ - 0 => __DIR__ . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'file1.txt', - 1 => __DIR__ . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'file2.txt'], + 0 => 'file1.exe', + 1 => 'file2.exe'], 'type' => [ 0 => 'plain/text', 1 => 'plain/text'], @@ -163,8 +163,8 @@ public function testMultiFiles() 0 => 8, 1 => 8], 'tmp_name' => [ - 0 => __DIR__ . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'file1.txt', - 1 => __DIR__ . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'file2.txt'], + 0 => __DIR__ . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'phpqBXGTg', + 1 => __DIR__ . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'phpZqRDQF'], 'error' => [ 0 => 0, 1 => 0]]]; @@ -172,8 +172,44 @@ public function testMultiFiles() $adapter->setOptions(['ignoreNoFile' => true]); $this->assertTrue($adapter->receive('exe')); $this->assertEquals( - ['exe_0_' => __DIR__ . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'file1.txt', - 'exe_1_' => __DIR__ . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'file2.txt'], + ['exe_0_' => 'phpqBXGTg_file1.exe', + 'exe_1_' => 'phpZqRDQF_file2.exe'], + $adapter->getFileName('exe', false)); + } + + + public function testMultiFilesSameName() + { + $_FILES = [ + 'txt' => [ + 'name' => 'test.txt', + 'type' => 'plain/text', + 'size' => 8, + 'tmp_name' => dirname(__FILE__) . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'php0zgByO', + 'error' => 0 + ], + 'exe' => [ + 'name' => [ + 0 => 'file.exe', + 1 => 'file.exe'], + 'type' => [ + 0 => 'plain/text', + 1 => 'plain/text'], + 'size' => [ + 0 => 8, + 1 => 8], + 'tmp_name' => [ + 0 => dirname(__FILE__) . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'phpOOwDDc', + 1 => dirname(__FILE__) . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'phpDlIxkx'], + 'error' => [ + 0 => 0, + 1 => 0]]]; + $adapter = new HttpTestMockAdapter(); + $adapter->setOptions(['ignoreNoFile' => true]); + $this->assertTrue($adapter->receive('exe')); + $this->assertEquals( + ['exe_0_' => 'phpOOwDDc_file.exe', + 'exe_1_' => 'phpDlIxkx_file.exe'], $adapter->getFileName('exe', false)); } diff --git a/test/Transfer/Adapter/_files/file2.txt b/test/Transfer/Adapter/_files/file2.txt deleted file mode 100644 index e7cbb71..0000000 --- a/test/Transfer/Adapter/_files/file2.txt +++ /dev/null @@ -1 +0,0 @@ -testfile \ No newline at end of file diff --git a/test/Transfer/Adapter/_files/file1.txt b/test/Transfer/Adapter/_files/php0zgByO similarity index 100% rename from test/Transfer/Adapter/_files/file1.txt rename to test/Transfer/Adapter/_files/php0zgByO diff --git a/test/Transfer/Adapter/_files/phpDlIxkx b/test/Transfer/Adapter/_files/phpDlIxkx new file mode 100644 index 0000000..2691857 --- /dev/null +++ b/test/Transfer/Adapter/_files/phpDlIxkx @@ -0,0 +1 @@ +testfile diff --git a/test/Transfer/Adapter/_files/phpOOwDDc b/test/Transfer/Adapter/_files/phpOOwDDc new file mode 100644 index 0000000..2691857 --- /dev/null +++ b/test/Transfer/Adapter/_files/phpOOwDDc @@ -0,0 +1 @@ +testfile diff --git a/test/Transfer/Adapter/_files/phpZqRDQF b/test/Transfer/Adapter/_files/phpZqRDQF new file mode 100644 index 0000000..2691857 --- /dev/null +++ b/test/Transfer/Adapter/_files/phpZqRDQF @@ -0,0 +1 @@ +testfile diff --git a/test/Transfer/Adapter/_files/phpqBXGTg b/test/Transfer/Adapter/_files/phpqBXGTg new file mode 100644 index 0000000..2691857 --- /dev/null +++ b/test/Transfer/Adapter/_files/phpqBXGTg @@ -0,0 +1 @@ +testfile