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".
Below is my code for sending just one file. Please feel free to ask any query.
$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' => 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);
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:
Array( [x1] => Array
(
[name] => Array
(
[0] => img1.png
)
[type] => Array
(
[0] => image/png
)
[tmp_name] => Array
(
[0] => C:\wamp\tmp\php6F66.tmp
)
[error] => Array
(
[0] => 0
)
[size] => Array
(
[0] => 6698
)
)
[x2] => Array
(
[name] => Array
(
[0] => img-2 - Copy.png
[1] => img-3.png
[2] => img-4.png
)
[type] => Array
(
[0] => image/png
[1] => image/png
[2] => image/png
)
[tmp_name] => Array
(
[0] => C:\wamp\tmp\php6F67.tmp
[1] => C:\wamp\tmp\php6F77.tmp
[2] => C:\wamp\tmp\php6F88.tmp
)
[error] => Array
(
[0] => 0
[1] => 0
[2] => 0
)
[size] => Array
(
[0] => 165091
[1] => 165091
[2] => 6698
)
)
[x3] => Array
(
[name] => Array
(
[0] => img-5 - Copy.png
[1] => img-6.png
[2] => img-6.png
)
[type] => Array
(
[0] => image/png
[1] => image/png
[2] => image/png
)
[tmp_name] => Array
(
[0] => C:\wamp\tmp\php6FA8.tmp
[1] => C:\wamp\tmp\php6FB9.tmp
[2] => C:\wamp\tmp\php6FD9.tmp
)
[error] => Array
(
[0] => 0
[1] => 0
[2] => 0
)
[size] => Array
(
[0] => 165091
[1] => 165091
[2] => 6698
)
)
)
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
Here is the Solution:
function uploadfile($tmp_value, $type_value, $imageName){
$url = 'http://localhost/target_url';
$ch = curl_init();
$fields_string = array(
'uploaded_file' => 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 => $images) {
foreach ($images['name'] as $imageData_key => $imageName){
foreach ($images['type'] as $type_key => $type_value) {
foreach ($images['tmp_name'] as $tmp_key => $tmp_value) {
}
}
echo $this->uploadfile($tmp_value, $type_value, $imageName);
}
}