欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

php curl 批量上传,并且带post 其它表单,多文件上传为什么不能再传其它post

程序员文章站 2022-04-20 14:54:17
...
php curl 批量上传,并且带 post 其它表单, 单个上传是可以跟 post 一起,可以批量的话就不行,有什么办法吗?
 'Foo', 'file'=>$file); //多文件上传为什么不能再传其它post

//变成
//$data = $file;


curl_setopt($ch, CURLOPT_URL, 'http://localhost/upload.php');
curl_setopt($ch, CURLOPT_POST, 1);


if (class_exists('\CURLFile')) {
     curl_setopt($ch, CURLOPT_SAFE_UPLOAD, true);
} else {
    if (defined('CURLOPT_SAFE_UPLOAD')) {
        curl_setopt($ch, CURLOPT_SAFE_UPLOAD, false);// 5.6 给改成 true了, 弄回去 
    }
}
 
curl_setopt($ch, CURLOPT_POSTFIELDS,$data);

curl_exec($ch);

//关闭cURL资源,并且释放系统资源
curl_close($ch);

?> 

最后运行的结果如何能象表单那样,又有 $_FILES 又有 $_POST 呢?

单文件是可以的,可是多文件就是不行,怎么办?

回复内容:

php curl 批量上传,并且带 post 其它表单, 单个上传是可以跟 post 一起,可以批量的话就不行,有什么办法吗?

 'Foo', 'file'=>$file); //多文件上传为什么不能再传其它post

//变成
//$data = $file;


curl_setopt($ch, CURLOPT_URL, 'http://localhost/upload.php');
curl_setopt($ch, CURLOPT_POST, 1);


if (class_exists('\CURLFile')) {
     curl_setopt($ch, CURLOPT_SAFE_UPLOAD, true);
} else {
    if (defined('CURLOPT_SAFE_UPLOAD')) {
        curl_setopt($ch, CURLOPT_SAFE_UPLOAD, false);// 5.6 给改成 true了, 弄回去 
    }
}
 
curl_setopt($ch, CURLOPT_POSTFIELDS,$data);

curl_exec($ch);

//关闭cURL资源,并且释放系统资源
curl_close($ch);

?> 

最后运行的结果如何能象表单那样,又有 $_FILES 又有 $_POST 呢?

单文件是可以的,可是多文件就是不行,怎么办?

我记得是必须给个文件名吧。不能传递数组过去。只能指定key。

$postfileds = [
    'file1' => CURLFile(xxxxx
    'file2' => CURLFile(xxxxxxx
    # 一定要用数组那也一定要这么写
    'file[]' => CURLFile(xxxxxx
    'file[]' => CURLFile(xxxxx
];

把file_path设置成一个数组 循环执行

相关标签: php