[PHP] CURL文件上传

一、说明

  本文主要简述CURL进行文件上传的一般操作,基于TP5框架;

  

二、前端

  代码如下,需要填入对应的上传地址还有修改接收的参数名字(这里是 file):

 <form  action="上传地址"  method="post" enctype="multipart/form-data">
        <input type="file" name="file">
        <button type="submit">上传</button>
    </form>

三、后端

  下面是基于TP5的上传处理,通过CURL上传到另外一台服务器上。

 1 <?php
 2 namespace app\controller;
 3
 4 use think\Controller;
 5
 6 //文件上传类
 7 class Upload extends Controller
 8 {
 9     protected $file_size = 20971520;//20M
10     protected $file_type = ["png", "jpg", "jpeg", "gif"];
11     protected $ret = [‘code‘=>0, ‘msg‘=>‘‘, ‘data‘=>[]];
12     private $uploadUrl = "http://xxx.com";//上传地址
13
14     public function doit()
15     {
16         try{
17             $verify = $file->validate([‘size‘=>$this->file_size,‘ext‘=>$this->file_type]);
18             if (!$verify) {
19                 throw new \Exception(‘上传的文件大小超过20M, 或文件类型不正确‘);
20             }
21
22             $ext = pathinfo($file->getInfo(‘name‘))[‘extension‘];
23             $tm = time();
24             $mime = $file->getInfo(‘type‘);
25
26             //表单请求参数
27             $postData = [
28                 ‘file‘ => new \CURLFile(realpath($file->getPathname()), $mime, $fileName.".{$ext}"),
29             ];
30
31             $curlRes = $this->curlUploadFile($this->uploadUrl, $postData);
32             $res = json_decode($curlRes, true);
33
34             if($res[‘code‘] == 200 && $res[‘data‘][‘filePath‘] != ""){
35                 $this->ret[‘code‘] = 200;
36                 $this->ret[‘msg‘] = ‘文件上传成功‘;
37                 $this->ret[‘data‘] = [‘filePath‘ => $res[‘data‘][‘filePath‘]];
38             }else{
39                 throw new \Exception(‘上传文件失败: ‘.$res[‘msg‘], 500);
40             }
41         }catch(\Exception $ex){
42             //异常处理
43             $this->ret[‘code‘] = 500;
44             $this->ret[‘msg‘] = $ex->getMessage();
45         }
46         return json($this->ret);//返回json
47     }
48
50
51     //CURL文件上传
52     private function curlUploadFile($url, $data)
53     {
54         $curl = curl_init();
55         if (class_exists(‘\CURLFile‘)) {
56             curl_setopt($curl, CURLOPT_SAFE_UPLOAD, true);
57             //$data = array(‘file‘ => new \CURLFile(realpath($path)));//>=5.5
58         } else {
59             if (defined(‘CURLOPT_SAFE_UPLOAD‘)) {
60                 curl_setopt($curl, CURLOPT_SAFE_UPLOAD, false);
61             }
62             //$data = array(‘file‘ => ‘@‘ . realpath($path));//<=5.5
63         }
64         curl_setopt($curl, CURLOPT_URL, $url);
65         curl_setopt($curl, CURLOPT_POST, 1 );
66         curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
67         curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
68         $result = curl_exec($curl);
69         $error = curl_error($curl);
70
71         curl_close($curl);
72
73         return $result;
74     }
75 }

原文地址:https://www.cnblogs.com/reader/p/11444608.html

时间: 2024-08-10 07:05:22

[PHP] CURL文件上传的相关文章

php curl文件上传兼容php5.0~5.6各版本

PHP 5.0~5.6 各版本兼容的cURL文件上传 最近做的一个需求,使用PHP cURL上传文件.踩坑若干,整理如下. 不同版本PHP之间cURL的区别 PHP的cURL支持通过给CURL_POSTFIELDS传递关联数组(而不是字符串)来生成multipart/form-data的POST请求. 传统上,PHP的cURL支持通过在数组数据中,使用“@+文件全路径”的语法附加文件,供cURL读取上传.这与命令行直接调用cURL程序的语法是一致的: 1 curl_setopt(ch, CURL

php5.6及以上版本利用curl文件上传

PHP的cURL支持通过给CURL_POSTFIELDS传递关联数组(而不是字符串)来生成multipart/form-data的POST请求. 传统上,PHP的cURL支持通过在数组数据中,使用“@+文件全路径”的语法附加文件,供cURL读取上传.这与命令行直接调用cURL程序的语法是一致的: curl_setopt(ch, CURLOPT_POSTFIELDS, array( 'file' => '@'.realpath('image.png'), )); equals $ curl -F

curl文件上传类

<form action="index.php" method="post" enctype='multipart/form-data' >     <input type="file" name="path" />     <input type="submit"></form><?php include 'imageUpload.php'; $too

php curl post上传文件

最近在写一个关于php发送日志的功能,需要一个后台上传的功能,需要用curl进行发送post请求,但是网络上面找了一些文章,经过测试却没有任何反应,以下是我经过实际测试通过的上传代码: /** *  curl文件上传 *  @var  struing  $r_file  上传文件的路劲和文件名 */ function upload_file($r_file) { /*$fields['uploadfile'] = "@".realpath($r_file);*/ $varname =

使用cURL POST上传文件

使用cURL POST上传文件 http://blog.csdn.net/v6543210/article/details/20152575

文件上传到百度云盘说明文档

图1 图2 图3 图4 1. 上传百度云盘功能,由于百度开发者中还没有开放对.net 操作的SDK,所以我们现在只能使用原生的REST API   我们的做法就是如何用C# 语言调用 调用curl 命令. 2. curl是利用URL语法在命令行方式下工作的开源文件传输工具.它被广泛应用在Unix.多种Linux发行版中,并且有DOS和Win32.Win64下的移植版本. 要操作curl 我们需要引入LibCurlNet.dll   3.百度上传我们需要有百度账号,而且需要申请开发者功能进入主页后

客户端的文件上传到服务器,服务器返回文件的路径

客户端的文件上传到服务器,服务器返回文件的路径 返回信息,客户端将文件保存 客户端: <?php header('content-type:text/html;charset=utf8'); $url = 'http://192.168.1.118/legcc/aaa.php';//访问的服务器的地址 $curl = curl_init(); $path = 'D:\www\ceshi\a02.jpeg';//客户端文件的绝对路径 $source = file_get_contents($pat

使用libcurl进行文件上传

上篇博文讲到了如何使用multicurl来进行http并发访问,今天继续有关curl的主题,来八一八如何使用curl来上传文件,在介绍具体方法之前了解下目前http文件上传的基本实现. rfc1867描述了如何使用http协议来上传客户端文件,目前基本上所有的浏览器和web服务器都支持http文件上传,它的使用也十分的简单,具体的来说就是在页面上创建一个form表单,表单的enctype属性为multipart/form-data,action为接收上传文件的cgi url,请求方式为post,

libCurl的文件上传

最近在需要使用curl的上传功能,使用libCurl来实现.因此,先使用curl命令操作,然后再使用libCurl实现. 基于Http协议的文件上传的标准方法是: 基于POST Form的文件上传  RFC1867. 这个方法使用非常广泛,这个RFC规定了FORM上传文件的标准方法,如下介绍了基于libcurl来开发upload功能. 开发实现过程 1. 使用curl 命令行执行代码,  2. 跟踪分析 curl的request和response, 3.使用libCurl的API进行开发实现 0