简体   繁体   English

用PHP curl发布文件数组

[英]Post file array with PHP curl

I want to post an array of files to a server using PHP and curl. 我想使用PHP和curl将文件数组发布到服务器。 In HTML it looks as follows: 在HTML中,其外观如下:

<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: 应该使用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. 但是,使用CURL时,服务器不会收到localfile数组。 What is the correct command for sending the files? 发送文件的正确命令是什么?

Regards, 问候,

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM