我需要使用curl php发送多个文件,但是问题是,要发送的文件数量因用户而异,因此我必须使用某种迭代方式来创建文件数组,然后使用“ curl_file_create”。
下面是我的仅发送一个文件的代码。请随时提出任何疑问。
$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);
此问题的补充。 因此,我有四个input(FILE)字段,并且用户可以为每个字段选择多个文件。因此,一旦用户选择了文件数组,如下所示:
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
)
)
)
现在,我需要知道如何使用curl发送所有这些图像,并且由于文件数不是恒定的,因此这里必须使用一些迭代。 -解决了
这是解决方案:
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);
}
}
答案 0 :(得分:0)
您可以尝试使用类似的东西
//为数组中的每个图像文件调用uploadfile函数
foreach ($imagesArray as $images_key => $images){
foreach ($images['name'] as $imageData_key => $imageName){
$response = uploadfile($imagesArray[$images_key][$imageData_key]['tmp_name'], $imagesArray[$images_key][$imageData_key]['type'], $imageName);
//do something with $response below
echo $response;
}
}
//上传功能:
function uploadfile($tmpfile, $type, $filename){
$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, $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);
return $response;
}