php模擬input 的file上傳文件

遇到一個需求是要把前端的上傳文件經過php拿到而後再上傳給接口,由於不能瀏覽器不能跨域上傳拿到返回值,因此只能用前端上傳,而後php拿到文件再上傳一次。以前在網上找到curl方式不能上傳二進制文件流,失敗了結,代碼以下:php

上傳函數:    前端

function curl_upload_file($url,$filename,$path,$type,$d){  
        if (class_exists('\CURLFile')) {  
            $data['file'] = new \CURLFile(realpath($path),$type,$filename);  
        } else {  
            $data['file'] = '@'.realpath($path).";type=「.$type.」;filename=".$filename;  
        }json

        $ch = curl_init();  
        curl_setopt($ch, CURLOPT_URL, $curl);  
        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);  
        return $return_data;  
    }api

函數使用:跨域

$file = $_FILES['file'];瀏覽器

$data = $this->curl_upload_file($url, $file['name'], $file['tmp_name'], $file['type'],$request);app

 

失敗了結。curl

 

以後又在網上找到了二進制文件流的方式來模擬上傳,終於成功,特寫上完整代碼函數

 

public function batchUpload(){post

        ini_set('upload_max_filesize', '10M');  
        ini_set('post_max_size', '10M');  
        ini_set('memory_limit', '128M'); 
        $request = array();
        $request['type'] = $req->getParameter('type','1');
        $request['userToken'] = $this->userToken;
        $request['apiKey'] = $this->apiKey;
        $url ="http:www.test.com/comdata/importExcelSuit?";
        $result = array();
        $result['success'] = 1;

        $allowtype = array("xlsx", "xls");
        $file = $_FILES['file'];
        $aryStr = explode(".", $file['name']);
        $allowsize = 10485760;
        if (!in_array(strtolower($aryStr[count($aryStr)-1]), $allowtype)) {
            $result['success'] = -1;
            $result['msg'] = "請上傳excel文件!";

        }
   
        if ($file['error'] != 0) {  
            $result['success'] = -1;  
            $result['msg'] = '上傳出錯';  
        }

        if ($file['size'] > $allowsize) {
            $result['success'] = -1;  
            $result['msg'] = '請上傳文件大小小於10M'; 
        } 

        if($result['success']!=-1){
            
            $fileurl = $url .'?'. http_build_query($request);
            $data = base::sendStreamFile($fileurl,$file['tmp_name']);
            $result['data'] = json_decode($data,true);
            if($result['data']['statuscode'] != 1){
                $result['success'] = -1;
                $result['msg'] =  $result['data']['dataInfo'];
            }else{

                $result['success'] = 1;
            }
            
        }

        echo json_encode($result);
     
    }

 上傳函數爲

    static function sendStreamFile($url,$file)  
    {  
        if (empty($url) || empty($file))  
        {  
            return false;  
        }  
        $opts = array(  
                'http' => array(  
                        'method' => 'POST',  
                        'header' => 'content-type:application/x-www-form-urlencoded',  
                        'content' => $file  
                )  
        );  
        $context = stream_context_create($opts);  
        $response = file_get_contents($url, false, $context);  
        return $response;  
          
    }  

 

全部上傳函數都是從網上down下來,感謝各位前輩的無私分享,由於來源比較雜,沒法標明出處,我只是代碼的搬運工。

相關文章
相關標籤/搜索