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

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

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Freigeben: 2016-06-06 20:07:12
Original
1623 Leute haben es durchsucht

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>
Nach dem Login kopieren
Nach dem Login kopieren

最后运行的结果如何能象表单那样,又有 $_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>
Nach dem Login kopieren
Nach dem Login kopieren

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

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

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

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

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

Verwandte Etiketten:
php
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Aktuelle Ausgaben
PHP-Datenerfassung?
Aus 1970-01-01 08:00:00
0
0
0
PHP-Erweiterung intl
Aus 1970-01-01 08:00:00
0
0
0
Wie man PHP gut lernt
Aus 1970-01-01 08:00:00
0
0
0
Mehrere PHP-Versionen
Aus 1970-01-01 08:00:00
0
0
0
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage