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

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

WBOY
Release: 2016-06-06 20:07:12
Original
1582 people have browsed it

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

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

?> </code>
Copy after login
Copy after login

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

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

回复内容:

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

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

?> </code>
Copy after login
Copy after login

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

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

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

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

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

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template