Post file array with PHP curl









up vote
0
down vote

favorite












I want to post an array of files to a server using PHP and curl. In HTML it looks as follows:



<form method="POST" type="multipart/form-data">
<input type="text" name="fieldx" value="123"/>
<input type="file" name="localfile"/>
<input type="file" name="localfile"/>
</form>


The same should be done with CURL:



$postParameters['fieldx'] = "123";
$postParameters['localfile'] = array("fulllocalfilepath1", "fulllocalfilepath2");

$request = curl_init('http://server.abc');
curl_setopt($request, CURLOPT_POST, true);
curl_setopt(
$request,
CURLOPT_POSTFIELDS,
$postParameters
);
curl_setopt($request, CURLOPT_RETURNTRANSFER, true);
$rawData = curl_exec($request);
curl_close($request);


But the server does not receive the localfile array when using CURL. What is the correct command for sending the files?



Regards,










share|improve this question

























    up vote
    0
    down vote

    favorite












    I want to post an array of files to a server using PHP and curl. In HTML it looks as follows:



    <form method="POST" type="multipart/form-data">
    <input type="text" name="fieldx" value="123"/>
    <input type="file" name="localfile"/>
    <input type="file" name="localfile"/>
    </form>


    The same should be done with CURL:



    $postParameters['fieldx'] = "123";
    $postParameters['localfile'] = array("fulllocalfilepath1", "fulllocalfilepath2");

    $request = curl_init('http://server.abc');
    curl_setopt($request, CURLOPT_POST, true);
    curl_setopt(
    $request,
    CURLOPT_POSTFIELDS,
    $postParameters
    );
    curl_setopt($request, CURLOPT_RETURNTRANSFER, true);
    $rawData = curl_exec($request);
    curl_close($request);


    But the server does not receive the localfile array when using CURL. What is the correct command for sending the files?



    Regards,










    share|improve this question























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I want to post an array of files to a server using PHP and curl. In HTML it looks as follows:



      <form method="POST" type="multipart/form-data">
      <input type="text" name="fieldx" value="123"/>
      <input type="file" name="localfile"/>
      <input type="file" name="localfile"/>
      </form>


      The same should be done with CURL:



      $postParameters['fieldx'] = "123";
      $postParameters['localfile'] = array("fulllocalfilepath1", "fulllocalfilepath2");

      $request = curl_init('http://server.abc');
      curl_setopt($request, CURLOPT_POST, true);
      curl_setopt(
      $request,
      CURLOPT_POSTFIELDS,
      $postParameters
      );
      curl_setopt($request, CURLOPT_RETURNTRANSFER, true);
      $rawData = curl_exec($request);
      curl_close($request);


      But the server does not receive the localfile array when using CURL. What is the correct command for sending the files?



      Regards,










      share|improve this question













      I want to post an array of files to a server using PHP and curl. In HTML it looks as follows:



      <form method="POST" type="multipart/form-data">
      <input type="text" name="fieldx" value="123"/>
      <input type="file" name="localfile"/>
      <input type="file" name="localfile"/>
      </form>


      The same should be done with CURL:



      $postParameters['fieldx'] = "123";
      $postParameters['localfile'] = array("fulllocalfilepath1", "fulllocalfilepath2");

      $request = curl_init('http://server.abc');
      curl_setopt($request, CURLOPT_POST, true);
      curl_setopt(
      $request,
      CURLOPT_POSTFIELDS,
      $postParameters
      );
      curl_setopt($request, CURLOPT_RETURNTRANSFER, true);
      $rawData = curl_exec($request);
      curl_close($request);


      But the server does not receive the localfile array when using CURL. What is the correct command for sending the files?



      Regards,







      php curl http-post multipartform-data php-curl






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked 2 days ago









      Hyndrix

      1,71052244




      1,71052244






















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          0
          down vote













          Got the solution:



          $postParameters['fieldx'] = "123";
          $postParameters['localfile[0]'] = new cURLFile("fulllocalfilepath1", "mime", "readable name 1");
          $postParameters['localfile[1]'] = new cURLFile("fulllocalfilepath2", "mime", "readable name 2");
          $request = curl_init('http://server.abc');
          curl_setopt($request, CURLOPT_POST, true);
          curl_setopt(
          $request,
          CURLOPT_POSTFIELDS,
          $postParameters
          );
          curl_setopt($request, CURLOPT_RETURNTRANSFER, true);
          $rawData = curl_exec($request);
          curl_close($request);





          share|improve this answer




















          • you can also do $postParameters['localfile']=>array((new cURLFile("fulllocalfilepath1", "mime", "readable name 1")), (new cURLFile("fulllocalfilepath2", "mime", "readable name 2")));
            – hanshenrik
            2 days ago










          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













           

          draft saved


          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53225179%2fpost-file-array-with-php-curl%23new-answer', 'question_page');

          );

          Post as a guest






























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          0
          down vote













          Got the solution:



          $postParameters['fieldx'] = "123";
          $postParameters['localfile[0]'] = new cURLFile("fulllocalfilepath1", "mime", "readable name 1");
          $postParameters['localfile[1]'] = new cURLFile("fulllocalfilepath2", "mime", "readable name 2");
          $request = curl_init('http://server.abc');
          curl_setopt($request, CURLOPT_POST, true);
          curl_setopt(
          $request,
          CURLOPT_POSTFIELDS,
          $postParameters
          );
          curl_setopt($request, CURLOPT_RETURNTRANSFER, true);
          $rawData = curl_exec($request);
          curl_close($request);





          share|improve this answer




















          • you can also do $postParameters['localfile']=>array((new cURLFile("fulllocalfilepath1", "mime", "readable name 1")), (new cURLFile("fulllocalfilepath2", "mime", "readable name 2")));
            – hanshenrik
            2 days ago














          up vote
          0
          down vote













          Got the solution:



          $postParameters['fieldx'] = "123";
          $postParameters['localfile[0]'] = new cURLFile("fulllocalfilepath1", "mime", "readable name 1");
          $postParameters['localfile[1]'] = new cURLFile("fulllocalfilepath2", "mime", "readable name 2");
          $request = curl_init('http://server.abc');
          curl_setopt($request, CURLOPT_POST, true);
          curl_setopt(
          $request,
          CURLOPT_POSTFIELDS,
          $postParameters
          );
          curl_setopt($request, CURLOPT_RETURNTRANSFER, true);
          $rawData = curl_exec($request);
          curl_close($request);





          share|improve this answer




















          • you can also do $postParameters['localfile']=>array((new cURLFile("fulllocalfilepath1", "mime", "readable name 1")), (new cURLFile("fulllocalfilepath2", "mime", "readable name 2")));
            – hanshenrik
            2 days ago












          up vote
          0
          down vote










          up vote
          0
          down vote









          Got the solution:



          $postParameters['fieldx'] = "123";
          $postParameters['localfile[0]'] = new cURLFile("fulllocalfilepath1", "mime", "readable name 1");
          $postParameters['localfile[1]'] = new cURLFile("fulllocalfilepath2", "mime", "readable name 2");
          $request = curl_init('http://server.abc');
          curl_setopt($request, CURLOPT_POST, true);
          curl_setopt(
          $request,
          CURLOPT_POSTFIELDS,
          $postParameters
          );
          curl_setopt($request, CURLOPT_RETURNTRANSFER, true);
          $rawData = curl_exec($request);
          curl_close($request);





          share|improve this answer












          Got the solution:



          $postParameters['fieldx'] = "123";
          $postParameters['localfile[0]'] = new cURLFile("fulllocalfilepath1", "mime", "readable name 1");
          $postParameters['localfile[1]'] = new cURLFile("fulllocalfilepath2", "mime", "readable name 2");
          $request = curl_init('http://server.abc');
          curl_setopt($request, CURLOPT_POST, true);
          curl_setopt(
          $request,
          CURLOPT_POSTFIELDS,
          $postParameters
          );
          curl_setopt($request, CURLOPT_RETURNTRANSFER, true);
          $rawData = curl_exec($request);
          curl_close($request);






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered 2 days ago









          Hyndrix

          1,71052244




          1,71052244











          • you can also do $postParameters['localfile']=>array((new cURLFile("fulllocalfilepath1", "mime", "readable name 1")), (new cURLFile("fulllocalfilepath2", "mime", "readable name 2")));
            – hanshenrik
            2 days ago
















          • you can also do $postParameters['localfile']=>array((new cURLFile("fulllocalfilepath1", "mime", "readable name 1")), (new cURLFile("fulllocalfilepath2", "mime", "readable name 2")));
            – hanshenrik
            2 days ago















          you can also do $postParameters['localfile']=>array((new cURLFile("fulllocalfilepath1", "mime", "readable name 1")), (new cURLFile("fulllocalfilepath2", "mime", "readable name 2")));
          – hanshenrik
          2 days ago




          you can also do $postParameters['localfile']=>array((new cURLFile("fulllocalfilepath1", "mime", "readable name 1")), (new cURLFile("fulllocalfilepath2", "mime", "readable name 2")));
          – hanshenrik
          2 days ago

















           

          draft saved


          draft discarded















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53225179%2fpost-file-array-with-php-curl%23new-answer', 'question_page');

          );

          Post as a guest














































































          Popular posts from this blog

          How to how show current date and time by default on contact form 7 in WordPress without taking input from user in datetimepicker

          Syphilis

          Darth Vader #20