简体   繁体   English

通过curl使用PHP上传多个文件

[英]Uploading multiple files using PHP via curl

I am trying to transfer two files via PHP using cURL. 我试图通过PHP使用cURL传输两个文件。 I know the methodology via the command line would be 我知道通过命令行的方法

curl -T "{file1,file2,..filex}" ftp://ftp.example.com/ -u username:password

And within php you can upload 1 file via the following code 在php中,您可以通过以下代码上传1个文件

$ch = curl_init();
$localfile = "somefile";
$username = "Someusername@someserver.com";
$password = "somerandomhash";
$fp = fopen($localfile, 'r');
curl_setopt($ch, CURLOPT_URL, "ftp://ftp.domain.com/");
curl_setopt($ch, CURLOPT_USERPWD, "$user:$pass");
curl_setopt($ch, CURLOPT_UPLOAD, 1);
curl_setopt($ch, CURLOPT_INFILE, $fp);
curl_setopt($ch, CURLOPT_INFILESIZE, filesize($localfile));
curl_exec ($ch);
$error_no = curl_errno($ch);
curl_close ($ch);

But how would I do multiple files using the above methodology? 但是,如何使用上述方法执行多个文件?

If you can use the same credentials to connect to FTP in parallel, you could try to do that with minimum changes via curl_multi_* with the code like this: 如果您可以使用相同的凭据并行连接到FTP,您可以尝试通过curl_multi_*进行最少的更改, curl_multi_*使用以下代码:

$chs = array();
$cmh = curl_multi_init();
for ($i = 0; $i < $filesCount; $i++)
{
    $chs[$i] = curl_init();
    // set curl properties
    curl_multi_add_handle($cmh, $chs[$i]);
}

$running=null;
do {
    curl_multi_exec($cmh, $running);
} while ($running > 0);

for ($i = 0; $i < $filesCount; $i++)
{
    $content = curl_multi_getcontent($chs[$t]);
    // parse reply
    curl_multi_remove_handle($cmh, $chs[$t]);
    curl_close($chs[$t]);
}
curl_multi_close($cmh);

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

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