php curl 批量上传,并且带post 其它表单,多文件上传为什么不能再传其它post
PHP中文网
PHP中文网 2017-04-10 17:50:35
[PHP讨论组]

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 呢?

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

PHP中文网
PHP中文网

认证高级PHP讲师

全部回复(3)
PHPz

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

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

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

黄舟

你可以看看我写的这个,支持多文件和$_POST http://blog.csdn.net/qq_29518...

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号