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
  • 相关阅读:
    HDU 2544 最短路
    Codeforces Round #358 (Div. 2) Alyona and Strings
    Codeforces Round #358 (Div. 2) Alyona and the Tree
    Codeforces Round #357 (Div. 2) Runaway to a Shadow
    pycharm快捷键和一些常用的设置
    Python不同功能的函数
    面向对象技术简介
    数据结构
    python学习笔记
    ORACLE基本语法
  • 原文地址:https://www.cnblogs.com/lichihua/p/10503222.html
Copyright © 2011-2022 走看看