• curl 文件上传


    curl_file_create (带路径的文件名 [, 文件mimetype , 上传数据里的文件名] ) ;

    new cURLFile (带路径的文件名 [, 文件mimetype , 上传数据里的文件名] ) ;

    $ch = curl_init('http://example.com/upload.php');
    // 创建CURLFile对象
    $cfile = curl_file_create('cats.jpg','image/jpeg','test_name');
    
    // 分配提交的数据
    $data = array('test_file' => $cfile);
    curl_setopt($ch, CURLOPT_POST,1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    curl_exec($ch);
    upload.php打印输出:
    array(1) {
      ["test_file"]=>
      array(5) {
        ["name"]=>
        string(9) "test_name"
        ["type"]=>
        string(10) "image/jpeg"
        ["tmp_name"]=>
        string(14) "/tmp/phpPC9Kbx"
        ["error"]=>
        int(0)
        ["size"]=>
        int(46334)
      }
    }
    View Code



       $ch = curl_init();
        // 上传多个
        $postFields = array(
            'file[0]' => new cURLFile($file1, $mimetype1, $basename1),
            'file[1]' => new cURLFile($file2, $mimetype2, $basename2)
        )
        
        curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);

    将@前缀文件名转为cURLFile

    if(is_array($postfields) == true)
    {
        foreach($postfields as $key => $value)
        {
            // 以@开头
            if(strpos($value, '@') === 0)
            {
                // 得到去掉@的文件名
                $filename = ltrim($value, '@');
                //转为CURLFile类
                $postfields[$key] = new CURLFile($filename);
            }
        }
    }
    curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields);
    //上传地址
    $target="http://youraddress.tld/example/upload.php";
    //面向过程的方式创建CURLFile 对象
    $cfile1 = curl_file_create('resource/test.png','image/png','testpic');  
    
    //面向对象 的方式创建CURLFile 对象
    $cfile2 = new CURLFile('resource/test.png','image/png','testpic'); 
    
    分配post提交的数据
    $imgdata =[
        'myimage1' => $cfile1,
        'myimage2' => $cfile2
    ];
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $target);
    //可选
    curl_setopt($curl, CURLOPT_USERAGENT,'Opera/9.80 (Windows NT 6.2; Win64; x64) Presto/2.12.388 Version/12.15');
    可选
    curl_setopt($curl, CURLOPT_HTTPHEADER,array('User-Agent: Opera/9.80 (Windows NT 6.2; Win64; x64) Presto/2.12.388 Version/12.15','Referer: http://someaddress.tld','Content-Type: multipart/form-data'));
    
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); // 停止验证证书
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);//将获取的信息以字符串返回 
    curl_setopt($curl, CURLOPT_POST, true); // post请求
    curl_setopt($curl, CURLOPT_POSTFIELDS, $imgdata); // 提交
    //可选
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true); // 上传后有重定向
    $r = curl_exec($curl); 
    curl_close($curl);
    View Code
  • 相关阅读:
    MOCK服务小结
    微信收款「商业版」与「个人版」有什么区别?看完这篇你就懂!
    LB 负载均衡的层次结构
    中兴应用之星携手天翼开放平台:让APP开发更简单
    常见面试题之二叉树
    【深入JAVA EE】Spring配置文件解析
    spring-struts-mybatis整合错误集锦
    Redis安装
    Atitit.ati dwr的原理and设计 attilax 总结 java php 版本号
    Android UI开发神兵利器之Icon
  • 原文地址:https://www.cnblogs.com/lichihua/p/10503222.html
Copyright © 2020-2023  润新知