Skip to content
This repository

Fixes problem with multi-dimensional files array #74

Merged
merged 1 commit into from over 1 year ago

2 participants

Martin Hasoň Fabien Potencier
Martin Hasoň

fixes #37

Fabien Potencier fabpot referenced this pull request from a commit August 16, 2012
Fabien Potencier merged branch hason/nested_files (PR #74)
Commits
-------

b207ffc Fixes problem with multi-dimensional files array

Discussion
----------

Fixes problem with multi-dimensional files array

fixes #37
6d26279
Fabien Potencier fabpot merged commit b207ffc into from August 16, 2012
Fabien Potencier fabpot closed this August 16, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 1 unique commit by 1 author.

Aug 16, 2012
Martin Hasoň Fixes problem with multi-dimensional files array b207ffc
This page is out of date. Refresh to see the latest.
30  Goutte/Client.php
@@ -19,6 +19,7 @@
19 19
 use Guzzle\Http\Message\Response as GuzzleResponse;
20 20
 use Guzzle\Http\ClientInterface as GuzzleClientInterface;
21 21
 use Guzzle\Http\Client as GuzzleClient;
  22
+use Guzzle\Http\Message\EntityEnclosingRequestInterface;
22 23
 
23 24
 /**
24 25
  * Client.
@@ -112,15 +113,7 @@ protected function doRequest($request)
112 113
         }
113 114
 
114 115
         if ('POST' == $request->getMethod()) {
115  
-            $postFiles = array();
116  
-            foreach ($request->getFiles() as $name => $info) {
117  
-                if (isset($info['tmp_name']) && '' !== $info['tmp_name']) {
118  
-                    $postFiles[$name] = $info['tmp_name'];
119  
-                }
120  
-            }
121  
-            if (!empty($postFiles)) {
122  
-                $guzzleRequest->addPostFiles($postFiles);
123  
-            }
  116
+            $this->addPostFiles($guzzleRequest, $request->getFiles());
124 117
         }
125 118
 
126 119
         $guzzleRequest->getCurlOptions()
@@ -144,6 +137,25 @@ protected function doRequest($request)
144 137
         return $this->createResponse($response);
145 138
     }
146 139
 
  140
+    protected function addPostFiles($request, array $files, $arrayName = '')
  141
+    {
  142
+        if (!$request instanceof EntityEnclosingRequestInterface) {
  143
+            return;
  144
+        }
  145
+
  146
+        foreach ($files as $name => $info) {
  147
+            if (!empty($arrayName)) {
  148
+                $name = $arrayName . '[' . $name . ']';
  149
+            }
  150
+
  151
+            if (isset($info['tmp_name']) && '' !== $info['tmp_name']) {
  152
+                $request->addPostFile($name, $info['tmp_name']);
  153
+            } elseif (is_array($info)) {
  154
+                $this->addPostFiles($request, $info, $name);
  155
+            }
  156
+        }
  157
+    }
  158
+
147 159
     protected function createResponse(GuzzleResponse $response)
148 160
     {
149 161
         return new Response($response->getBody(true), $response->getStatusCode(), $response->getHeaders()->getAll());
24  Goutte/Tests/ClientTest.php
@@ -122,6 +122,30 @@ public function testUsesPostFiles()
122 122
         ), $request->getPostFiles());
123 123
     }
124 124
 
  125
+    public function testUsesPostFilesNestedFields()
  126
+    {
  127
+        $guzzle = $this->getGuzzle();
  128
+        $client = new Client();
  129
+        $client->setClient($guzzle);
  130
+        $files = array(
  131
+            'form' => array(
  132
+                'test' => array(
  133
+                    'name' => 'test.txt',
  134
+                    'tmp_name' => __FILE__
  135
+                ),
  136
+            ),
  137
+        );
  138
+
  139
+        $crawler = $client->request('POST', 'http://www.example.com/', array(), $files);
  140
+        $request = $this->historyPlugin->getLastRequest();
  141
+
  142
+        $this->assertEquals(array(
  143
+            'form[test]' => array(
  144
+                new PostFile('form[test]', __FILE__, 'text/x-php')
  145
+            )
  146
+        ), $request->getPostFiles());
  147
+    }
  148
+
125 149
     public function testUsesCurlOptions()
126 150
     {
127 151
         $guzzle = $this->getGuzzle();
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.