惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

D
DataBreaches.Net
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
云风的 BLOG
云风的 BLOG
B
Blog
博客园 - Franky
I
InfoQ
A
About on SuperTechFans
博客园_首页
L
LangChain Blog
量子位
腾讯CDC
Microsoft Security Blog
Microsoft Security Blog
博客园 - 【当耐特】
美团技术团队
V
V2EX
Apple Machine Learning Research
Apple Machine Learning Research
雷峰网
雷峰网
MongoDB | Blog
MongoDB | Blog
Microsoft Azure Blog
Microsoft Azure Blog
月光博客
月光博客
T
The Blog of Author Tim Ferriss
P
Proofpoint News Feed
G
Google Developers Blog
Last Week in AI
Last Week in AI

秋码分享

我是如何解决将 c++ 编译成可以在 node.js 中使用的 *.node,中间出现的一大堆问题的(指纹浏览器基石篇) eSIM Plus 爱沙尼亚手机号彻底翻车?“永久有效”悄然变成了一年! 接码平台 SMS-Activate 余额可以转移到新平台使用,截止日期:2026年1月29日 是时候将 hugo-theme-kiwi 主题提交到 themes.gohugo.io 站点上了 Flux2 刚开源就凉了?Z-Image 本地部署狠狠打了个样 声音的未来:Chatterbox —— 用「夸张度旋钮」提升表现力的开源 TTS 向导 还以为那只是换个背景?Qwen-Image-Edit 在 ComfyUI 中能做到更离谱的事 Windows 结合最新版 ComfyUI 部署阿里最新开源的 Qwen-Image 图像大模型 从零样本到跨场景:Seed-VC语音转换技术的革命性突破 大语音模型轻量化革命:MegaTTS3 如何重新定义文本生成语音的技术边界(windows篇) 竞赛级编程大模型OlympicCoder-7B之本地部署(Windows篇) 阿里开源了端到端全模态大模型Qwen-2.5-Omini-7B之本地部署(windows篇) 语音识别之whisper本地部署(实时语音之开篇) 甭管是个人还是企业都能部署的Mistral-Small3.1,远超同级别的模型 文生音乐开源项目DiffRhythm,8G显存本地部署之Windows篇 阿里QwQ-32B本地部署指南:用Ollama轻松运行320亿参数大模型 基于Qwen2.5大模型的Spark-TTS,零样本语音克隆,CPU可运行之本地部署(Windows篇) 智谱开源了文生图CogView4-6B模型,支持中文提示词之本地部署(Windows篇) 基于歌词生成整首歌的开源AI音乐模型,支持中、英、日、韩等多种语言,本地化部署YuE(windows篇) 阿里云开源的文生视频万相 Wan2.1之本地部署Wan2.1-T2V-1.3B模型 互动式开源AI图像编辑神器,Windows11本地部署 MagicQuill 本地部署Qwen2.5-VL-7B-Instruct多模态视觉大模型(Windows篇) 保持角色一致性的绘本生成AI开源项目之Story-Adapter本地部署Windows篇 本地部署 Stable Diffusion 3.5(最新 ComfyUI记录篇) 谁说Win7安装不了Node.js最新版的呢?都2025年,还不更新系统到Win11 vs code远程调试Linux服务器上的php代码 浏览器定制 | Windows11 编译 Chromium 133.0.6885.0(截稿前Chromium最新版之编译篇[一]) 不说是彻底搞懂,至少让你不再惧怕c/c++指针,以及各种奇葩指针变种 解决windows下php8.x及以上版本,在Apache2.4中无法加载CURL扩展的问题 在 Windows8.1 下编译 Chromium (103.0.5060.68 之三)
PHP使用curl上传文件到远程服务器接口
一个游离于山间之上的江湖漫行者。A jungle wanderer who wanders above the mountain · 2021-04-11 · via 秋码分享

PHP使用CURL上传文件到远程服务接口

当然你的php.ini开启了curl扩展功能

1、文件上传

  /**
     * 以文件上传  第二种方法
     */
    public function upload(){
        $result = array('code' => 0, 'message' => 'ok');
        ini_set('upload_max_filesize', '20M');
        ini_set('post_max_size', '20M');
        ini_set('memory_limit', '128M');

        $url="http://xx.xx.xx.xx/api/file/upload";  //远程接口地址

        $file = $_FILES['file'];

        if ($file['error'] != 0) {
            $result['code'] = 40001;
            $result['message'] = '上传出错';
            $this->error();  //使用Thinkphp5.1内置controller方法
        } else {
            $filename = $file['name'];
            $tmpfile = $file['tmp_name'];
            $filetype = $file['type'];
            $data = self::upload_file($url, $filename, $tmpfile, $filetype);

            $data = json_decode($data,true);
            $this->success(); ////使用Thinkphp5.1内置controller方法
            die;
        }
    }

 /**
  * curl上传文件
  * 
  * @param unknown $url
  * @param unknown $filename
  * @param unknown $path
  * @param unknown $type
 */
 protected function upload_file($url,$filename,$path,$type){
        //php 5.5以上的用法
    if (class_exists('\CURLFile')) {
       $data = array('file' => new \CURLFile(realpath($path),$type,$filename));
    } else {
       $data = array(
           'file'=>'@'.realpath($path).";type=".$type.";filename=".$filename
          );
    }
     $ch = curl_init();
     curl_setopt($ch, CURLOPT_URL, $url);
     curl_setopt($ch, CURLOPT_POST, true );
     curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
     curl_setopt($ch, CURLOPT_HEADER, false);
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
     $return_data = curl_exec($ch);
     curl_close($ch);
     echo $return_data;
    }

2、文件接收端(不限定于PHP)

 /**
   * 接收文件
  */
public function upload(){
    Log::info('--------------$_FILES--------------------');
    Log::info($_FILES);
    if( $_FILES){
        $filename = $_FILES['file']['name'];
        $tmpname = $_FILES['file']['tmp_name'];
        if(move_uploaded_file($tmpname,  $_SERVER['DOCUMENT_ROOT']."/excel-file/" .$filename)){
            echo json_encode('上传成功');
        }else{
            $data = json_encode($_FILES);
            echo $data;
        }
    }   
 }

3、以下是以二进制文件上传偶尔会失败,故而不推荐使用,在此记录下!

 /**
  * 以二进制文件上传
  */
 public function upload_stream(){

     $url="http://xx.xx.xx.xx/api/file/upload";  //上传远程接口地址
     $info = $_FILES['file'];
     $fp = fopen($info['tmp_name'], 'r');
     $filebinary = fread($fp, filesize($info['tmp_name']));
     fclose($fp);

     $opts = array(
         'http' => array(
             'method' => 'POST',
             'header' => 'content-type:application/x-www-form-urlencoded',
             'context' =>  $filebinary 
         )
     );

     $context = stream_context_create($opts);
     $response = file_get_contents($url, false, $context); //就是这个方法  偶尔会失败哦!
     $ret = json_decode($response, true);
     return $ret['success'];

 }

//以下服务部署于服务端(不限定于PHP)
 /**
  * 接收二进制流文件
 */
public function upload_stream(){

    $streamData = isset($GLOBALS['HTTP_RAW_POST_DATA']) ?                                          $GLOBALS['HTTP_RAW_POST_DATA'] : '';

    $receiveFile =  $_SERVER['DOCUMENT_ROOT']."/excel-file/receiv.xlsx";

    if(empty($streamData)){
        $streamData = file_get_contents('php://input');
    }

    if($streamData!=''){
        $ret = file_put_contents($receiveFile, $streamData, true);
    }else{
        $ret = false;
    }

    return $ret;
}