[PHP] CURL file upload

I. Description

  This review discusses general CURL file upload operations, based TP5 frame;

  

Second, the front end

  Code below, there is need to fill upload address corresponding to the received modified parameter names (here File ):

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

 

 

Third, the back-end

  The following is based on the upload process TP5, uploaded to another server by 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";upload address//
13 is  
14      public  function doIt ()
 15      {
 16          the try {
 . 17              $ Verify = $ File -> the validate ([ 'size' => $ the this -> FILE_SIZE, 'EXT' => $ the this -> FILE_TYPE]);
 18 is              IF ( ! $ Verify ) {
 . 19                  the throw  new new \ Exception ( 'upload file size exceeds 20M, or incorrect file type' );
 20 is              }
 21 is  
22 is              $ EXT = the pathinfo ( $ file -> getInfo ( 'name')) [ 'Extension ' ];
 23 is              $ (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             $resOf json_decode = ( $ curlRes , to true );
 33 is  
34 is              IF ( $ RES [ 'code'] == 200 is && $ RES ! [ 'Data'] [ 'filePath'] = "" ) {
 35                  $ the this -> RET [ ' code '] = 200 is ;
 36                  $ the this -> RET [' MSG '] =' file uploaded successfully ' ;
 37 [                  $ the this -> RET [' Data '] = [' filePath '=> $ RES [' Data '] [ 'filePath' ]];
 38 is              } the else {
 39                  the throw  new new \ Exception ( 'files that failed:'.$res['msg'], 500);
40             }
 41 is          } the catch (\ Exception  $ EX ) {
 42 is              // exception handler 
43 is              $ the this -> RET [ 'code'] = 500 ;
 44 is              $ the this -> RET [ 'MSG'] = $ EX -> the getMessage ();
 45          }
 46 is          return JSON ( $ the this -> RET); // returns JSON 
47      }
 48 
50  
51 is      // the CURL file upload 
52 is      Private  function curlUploadFile ( $ URL , $ Data )
 53 is      {
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 }

 

Guess you like

Origin www.cnblogs.com/reader/p/11444608.html