title: send multiple files data using curl php ( number of files to be sent varies )
body: <p>I need to send multiple files using curl php, but the issue, is the number of files to be sent is varies on the user, hence I have to use some kind of iteration to create array of files to send it using "curl_file_create".</p>
  
  <p>Below is my code for sending just one file. Please feel free to ask any query. </p>
  
  <pre><code>        $url = 'http://localhost/target_url';
          $ch = curl_init();
          // Loop over files
          $tmpfile = $_FILES['input_name']['tmp_name'];
          $filename = basename($_FILES['input_name']['name']);
          $fields_string =  array(
              'uploaded_file' =&gt; curl_file_create($tmpfile, $_FILES['input_name']['type'], $filename));
  
          curl_setopt($ch, CURLOPT_URL, $url);
          curl_setopt($ch, CURLOPT_POST, true);   
          curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
          curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
          $response_json = curl_exec($ch);
          curl_close($ch);
          $response=json_decode($response_json, true);
  </code></pre>
  
  <p>Addition to this question. 
  So I have four input( FILE ) fields, and a user can select multiple files for each of those fields. So, once the user has selected the array of files something like this: </p>
  
  <pre><code>Array( [x1] =&gt; Array
          (
              [name] =&gt; Array
                  (
                      [0] =&gt; img1.png
                  )
  
              [type] =&gt; Array
                  (
                      [0] =&gt; image/png
                  )
  
              [tmp_name] =&gt; Array
                  (
                      [0] =&gt; C:\wamp\tmp\php6F66.tmp
                  )
  
              [error] =&gt; Array
                  (
                      [0] =&gt; 0
                  )
  
              [size] =&gt; Array
                  (
                      [0] =&gt; 6698
                  )
  
          )
  
      [x2] =&gt; Array
          (
              [name] =&gt; Array
                  (
                      [0] =&gt; img-2 - Copy.png
                      [1] =&gt; img-3.png
                      [2] =&gt; img-4.png
                  )
  
              [type] =&gt; Array
                  (
                      [0] =&gt; image/png
                      [1] =&gt; image/png
                      [2] =&gt; image/png
                  )
  
              [tmp_name] =&gt; Array
                  (
                      [0] =&gt; C:\wamp\tmp\php6F67.tmp
                      [1] =&gt; C:\wamp\tmp\php6F77.tmp
                      [2] =&gt; C:\wamp\tmp\php6F88.tmp
                  )
  
              [error] =&gt; Array
                  (
                      [0] =&gt; 0
                      [1] =&gt; 0
                      [2] =&gt; 0
                  )
  
              [size] =&gt; Array
                  (
                      [0] =&gt; 165091
                      [1] =&gt; 165091
                      [2] =&gt; 6698
                  )
  
          )
  
      [x3] =&gt; Array
          (
              [name] =&gt; Array
                  (
                      [0] =&gt; img-5 - Copy.png
                      [1] =&gt; img-6.png
                      [2] =&gt; img-6.png
                  )
  
              [type] =&gt; Array
                  (
                      [0] =&gt; image/png
                      [1] =&gt; image/png
                      [2] =&gt; image/png
                  )
  
              [tmp_name] =&gt; Array
                  (
                      [0] =&gt; C:\wamp\tmp\php6FA8.tmp
                      [1] =&gt; C:\wamp\tmp\php6FB9.tmp
                      [2] =&gt; C:\wamp\tmp\php6FD9.tmp
                  )
  
              [error] =&gt; Array
                  (
                      [0] =&gt; 0
                      [1] =&gt; 0
                      [2] =&gt; 0
                  )
  
              [size] =&gt; Array
                  (
                      [0] =&gt; 165091
                      [1] =&gt; 165091
                      [2] =&gt; 6698
                  )
  
          )
  
  )
  </code></pre>
  
  <p>Now I need to know how I can send all these images using curl and since the number of files is not constant, hence there has to be some iteration to be used here. - Solved</p>
  
  <p>Here is the Solution: </p>
  
  <pre><code>function uploadfile($tmp_value, $type_value, $imageName){
              $url = 'http://localhost/target_url';
              $ch = curl_init();
  
              $fields_string =  array(
                  'uploaded_file' =&gt; curl_file_create($tmp_value, $type_value, $imageName));
  
              curl_setopt($ch, CURLOPT_URL, $url);
              curl_setopt($ch, CURLOPT_POST, true);   
              curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
              curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
              $response_json = curl_exec($ch);
              curl_close($ch);
              $file_response=json_decode($response_json, true);
              return $response_json;
          } 
  
  
  foreach ($_FILES as $images_key =&gt; $images) {
              foreach ($images['name'] as $imageData_key =&gt; $imageName){
  
                  foreach ($images['type'] as $type_key =&gt; $type_value) {
  
                      foreach ($images['tmp_name'] as $tmp_key =&gt; $tmp_value) {
  
                      }
  
                  }
  
                  echo $this-&gt;uploadfile($tmp_value, $type_value, $imageName);
  
              }
          }
  </code></pre>