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

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

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

<?php

//显示所有错误,除了注意的提示
error_reporting(E_ALL & ~E_NOTICE);    
$ch = curl_init();

$file_path='D:\1.jpg';

if(class_exists('\CURLFile')){
    $file[] = new \CURLFile(realpath($file_path));
    $file[] = new \CURLFile(realpath($file_path));
}else{
    $file[] = '@' . realpath($file_path);
    $file[] = '@' . realpath($file_path);
}

$data = array('name' => '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 呢?

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


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


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

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