zoukankan      html  css  js  c++  java
  • PHP curl上传图片

    function sendPost($url, $path)
    {
        $curl = curl_init();
        if (class_exists('CURLFile')) {
            //PHP版本 >= 5.5
            curl_setopt($curl, CURLOPT_SAFE_UPLOAD, true);
            $suffixIndex = strripos($path, '.');
            if($suffixIndex === false){
                $suffix = 'jpg';
            } else {
                $suffix = substr($path, $suffixIndex+1);
            }
            $filename = time() . rand(1000, 9999);
            $data = array('file' => new CURLFile(realpath($path),'image/' . $suffix, $filename .'.'.  $suffix));
        } else {
            if (defined('CURLOPT_SAFE_UPLOAD')) {
                curl_setopt($curl, CURLOPT_SAFE_UPLOAD, false);
            }
            $data = array('file' => '@' . realpath($path));//<=5.5
        }
        curl_setopt($curl, CURLOPT_SAFE_UPLOAD, true);
    
    
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_USERAGENT, "TEST");
        $result = curl_exec($curl);
        curl_close($curl);
        return $result;
    }
    
    $url = 'http://localhost:8080/upload';
    $path = "D:/phpstudy/PHPTutorial/WWW/test/timg23232.png";
    
    $res = sendPost($url, $path);
    var_dump($res);
    

      

  • 相关阅读:
    background和background-size
    获取表单的初始值,模拟placeholder属性
    input[type=checkbox]
    background-size
    input的type属性的修改
    选项卡切换
    2016.12.13
    3. 如何封装查询条件与查询结果到map中
    Java 实现网站当前在线用户统计
    sell-- wordPOI
  • 原文地址:https://www.cnblogs.com/f-rt/p/13043341.html
Copyright © 2011-2022 走看看