PHP 上传文件到其他服务器

PHP 上传文件到其他服务器

标签(空格分隔):

安装Guzzle类库

**guzzle**  是发送网络请求的类库
composer安装:**composer require guzzlehttp/guzzle**
中文文档[**https://guzzle-cn.readthedocs.io/zh_CN/latest/quickstart.html**]

文件上传

<?php
    use GuzzleHttp\Client;
    
    public $upload_type;  // 发送类型 'image|video|file'
    
    public function shopUpload ($name)
    {
        $file = fopen($_FILES[$name]['tmp_name'], 'r')
        $response = (new Client())->request(
            'POST',    //post 请求
            'http://www.xxx.cm/upload',
            [
                // name: (必须,字符串) 映射到表单字段的名称。
                // contents: (必须,混合) 提供一个字符串,可以是 fopen 返回的资源、或者一个
                
                'multipart' => [   //设置
                    [
                        'name'     => $name,
                        'contents' => $file,
                        'filename' => $_FILES[$name]['name'],
                        'type' => $_FILES[$name]['type']
                    ],
                    [
                        'name' => 'file_name',   // 上传表单的name值
                        'contents' => $name
                    ],
                    [
                        'name' => 'upload_type',  // 上传文件类型
                        'contents' => $this->upload_type,
                    ],
                ]
            ]
        );
        $resultMap = json_decode($response->getBody()->getContents(), true);
    }
    
    
    

服务器接收

function upload ()
{
    直接获取 $_FILES;
}

转载于:https://www.cnblogs.com/yanweifeng/p/11066035.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值