php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #76216 Post not working with subarray and file
Submitted: 2018-04-13 07:49 UTC Modified: 2018-04-13 08:20 UTC
From: standa dot david at gmail dot com Assigned:
Status: Duplicate Package: cURL related
PHP Version: 7.2.4 OS: Debian
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
MUST BE VALID
Solve the problem:
33 - 31 = ?
Subscribe to this entry?

 
 [2018-04-13 07:49 UTC] standa dot david at gmail dot com
Description:
------------
do you have any idea, how to post file with subarray form fields

Simple example: I need to have in form multiple var[] variables. So I build post array:

$curlfile = curl_file_create($filename);
$postdata = array('file'=> $curlfile,
                  'var' => array('value1', 'value2')
            );

curl_setopt($ch,CURLOPT_POSTFIELDS, $postdata);
Then I will make simple post request, but it is not working. When I inspect the result with https://httpbin.org/post or print_r($_POST) variable on my site, it will show only

Array
(
    [var] => Array
)

I need to have in post request var[]=value1&var[]=value2

Test script:
---------------
$curlfile = curl_file_create($filename);
$postdata = array('file'=> $curlfile,
                  'var' => array('value1', 'value2')
            );


$ch = curl_init ("https://httpbin.org/post");
curl_setopt($ch,CURLOPT_POST, true);
curl_setopt($ch,CURLOPT_POSTFIELDS, $postdata);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec($ch);

Expected result:
----------------
  "form": {
    "var[]": [
      "value1",
      "value2"
    ]
  },


Actual result:
--------------
  "form": {
    "var": "Array",
  },


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2018-04-13 08:19 UTC] requinix@php.net
-Summary: Post not working with subarray and file +Summary: CURLOPT_POSTFIELDS should support key[] syntax for array values -Type: Bug +Type: Feature/Change Request
 [2018-04-13 08:19 UTC] requinix@php.net
Duplicate of bug #51634, #67585, and others.

Workaround:

Normally constructing the POST data as a string would be easy, but that doesn't work for multipart bodies.
Use var[0], var[1], etc. as array keys.

$postdata = array(
    'file'=> $curlfile,
    'var[0]' => 'value1',
    'var[1]' => 'value2'
);

It's not literally the same but if you have PHP on the receiving end too then it won't make a difference.
 [2018-04-13 08:20 UTC] requinix@php.net
-Summary: CURLOPT_POSTFIELDS should support key[] syntax for array values +Summary: Post not working with subarray and file -Status: Open +Status: Duplicate -Type: Feature/Change Request +Type: Bug
 [2018-04-13 08:20 UTC] requinix@php.net
(oops)
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Apr 25 08:01:28 2024 UTC