Blogger Information
Blog 7
fans 1
comment 0
visits 11002
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
php curl 模拟表单提交上传文件
漫漫人生路
Original
849 people have browsed it

直接上代码

// $source 文件绝对路径
static public function upload($source)
{
    $url =  '上传的地址';

    $curl = curl_init();

    // php5.5以上不准用@ 而用CURLFile类
    if (class_exists('\CURLFile')) {
        curl_setopt($curl, CURLOPT_SAFE_UPLOAD, true);
        $data = array('file' => new \CURLFile(realpath($source)));//php版本>=5.5
    } else {
        if (defined('CURLOPT_SAFE_UPLOAD')) {
            curl_setopt($curl, CURLOPT_SAFE_UPLOAD, false);
        }
        $data = array('file' => '@' . realpath($source));//php版本<=5.5
    }

    $size = filesize($source);

    // 详情 请查看 curl_setopt()
    curl_setopt($curl, CURLOPT_HEADER, false); // 是否带头信息
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_POST, 1 ); // 设置post请求
    curl_setopt($curl, CURLOPT_POSTFIELDS, $data); // post 请求数据
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl, CURLOPT_INFILESIZE, $size);
    $result = curl_exec($curl);

    curl_close($curl);
    return json_decode($result, true);
}


Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post