zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    hi.baidu.com 百度流量统计
    Autofac is designed to track and dispose of resources for you.
    IIS Manager could not load type for module provider 'SharedConfig' that is declared in administration.config
    How to create and manage configuration backups in Internet Information Services 7.0
    定制swagger的UI
    NSwag在asp.net web api中的使用,基于Global.asax
    NSwag Tutorial: Integrate the NSwag toolchain into your ASP.NET Web API project
    JS变量对象详解
    JS执行上下文(执行环境)详细图解
    JS内存空间详细图解
  • 原文地址:https://www.cnblogs.com/lichihua/p/10503222.html
Copyright © 2011-2022 走看看