php curl上傳文件和參數(shù)

1.根據(jù)文件路徑上傳

<?php

$url = "http://localhost/file/upload";
$post_data = array(
  "filename" => "abc.png",
  "bar" => "foo",
  //要上傳的本地文件地址
  "file" = > "@/User/www/test/abc.png"
);
$ch = curl_init();
curl_setopt($ch , CURLOPT_URL , $url);
curl_setopt($ch , CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch , CURLOPT_POST, 1);
curl_setopt($ch , CURLOPT_POSTFIELDS, $post_data);
$output = curl_exec($ch);
curl_close($ch);
echo $output;

上傳文件需要用@加上文件的絕對(duì)路徑

2.根據(jù)文件內(nèi)容上傳

<?php

class CurlUploadFile
{   
    protected static $url;
    protected static $delimiter;
    protected static $instance;

    public function __construct() {
        //上傳地址
        static::$url = 'http://localhost/file/upload';
        //分割符
        static::$delimiter = uniqid();
    }

    public function putFile($param) {
        $post_data = static::buildData($param);
        $curl = curl_init(static::$url);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_POST, true);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data);
        curl_setopt($curl, CURLOPT_HTTPHEADER, [
            "Content-Type: multipart/form-data; boundary=" . static::$delimiter,
            "Content-Length: " . strlen($post_data)
        ]);
        $response = curl_exec($curl);
        curl_close($curl);
        $info = json_decode($response, true);
        return $response;
    }

    private static function buildData($param){
        $data = '';
        $eol = "\r\n";
        $upload = $param['file'];
        unset($param['file']);

        foreach ($param as $name => $content) {
            $data .= "--" . static::$delimiter . "\r\n"
                . 'Content-Disposition: form-data; name="' . $name . "\"\r\n\r\n"
                . $content . "\r\n";
        }
        // 拼接文件流
        $data .= "--" . static::$delimiter . $eol
            . 'Content-Disposition: form-data; name="file"; filename="' . $param['filename'] . '"' . "\r\n"
            . 'Content-Type:application/octet-stream'."\r\n\r\n";

        $data .= $upload . "\r\n";
        $data .= "--" . static::$delimiter . "--\r\n";
        return $data;
    }

    public static function getInstance() {
        if(!static::$instance){
            static::$instance = new static();
        }
       return static::$instance;
    }

}

調(diào)用上傳

$data = array(
    'type' => 'image',
    'filename' => 'abc.png',
    'filesize' => 58701, //分片上傳
    'offset' => 0,
    'filetype' => 'image/png',
    'originName' => 'abc.png',
    'file'=>file_get_contents('abc.png')
);
$part = CurlUploadFile::getInstance()->putFile($data);
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • .bat腳本基本命令語法 目錄 批處理的常見命令(未列舉的命令還比較多,請(qǐng)查閱幫助信息) 1、REM 和 :: 2...
    慶慶慶慶慶閱讀 8,539評(píng)論 1 19
  • Ubuntu的發(fā)音 Ubuntu,源于非洲祖魯人和科薩人的語言,發(fā)作 oo-boon-too 的音。了解發(fā)音是有意...
    螢火蟲de夢(mèng)閱讀 100,748評(píng)論 9 468
  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,654評(píng)論 19 139
  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,219評(píng)論 25 708
  • 上周《第一財(cái)經(jīng)周刊》發(fā)布了今年的新一線城市排名,對(duì)于各個(gè)城市的排名,大家應(yīng)該并沒有感到意外。四個(gè)一線城市的位置穩(wěn)穩(wěn)...
    馬虎眼閱讀 5,458評(píng)論 3 8

友情鏈接更多精彩內(nèi)容