php上传文件,通过curl上传到远程服务器 (解决php5.5 以上环境提交文件报错问题)

admin9年前程序心得1655
 /**
     * 上传图片api
     * 
     */
    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 = isset($_POST['url']) ? trim($_POST['url']) : '';
        if (empty($url)) {
            $result['code'] = 40001;
            $result['message'] = 'url不能为空';
            Helper_Http::writeJson(200,$result);
        }
        $file = $_FILES['file'];
        if ($file['error'] != 0) {
            $result['code'] = 40001;
            $result['message'] = '上传出错';
            Helper_Http::writeJson(200,$result);
        } else {
            $filename = $file['name'];
            $tmpfile = $file['tmp_name'];
            $filetype = $file['type'];
            $data = $this->upload_file($url, $filename, $tmpfile, $filetype);
            echo $data;
            die;
        }
    }
    
    
    /**
     * curl上传文件
     * 
     * @param unknown $url
     * @param unknown $filename
     * @param unknown $path
     * @param unknown $type
     */
    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;
    }
 远程代码的处理逻辑跟上传文件逻辑一样,这中间其实是有两部上传。
php上传文件,通过curl上传到远程服务器


分享到:

相关文章

jQuery ajax使用$(this).parent()无效解决方法

本文章来给各位同学介绍一下关于jQuery ajax使用$(this).parent()无效解决方法,希望此方法对各位同学人有所帮助哦html 代码如下复制代码<p class=&qu...

基于 Layui form 组件的省市区级联的实现 ---

<!DOCTYPE html> <html> <head>     <meta charse...

curl_multi实现并发

普通请求curl_normal.phpCopy<?php $srart_time = microtime(TRUE); $chArr=[];//创建多个cUR...

php解析crontab时间格式

crontab 时间格式:配置说明“* * * * * *”分 时 日 月 周“0 3 * * * *”数字精确配置, 星号为任意.(每天凌晨3点整)“15,30 3 * * *”逗号表示枚举 (每天...

多行文字溢出[...]的实现(text-overflow: ellipsis)

多行文字溢出[...]的实现(text-overflow: ellipsis)

对于单行文字, 很简单. Css代码  .oneLine {      width: 20...

微信支付 总提示get_brand_wcpay_request:fail 也不跳转支付页面 的解决方案

微信支付 总提示get_brand_wcpay_request:fail 也不跳转支付页面 的解决方案

最近在做微信支付,帮客户部署好环境后,测试微信支付,发现点击支付后老是提示:get_brand_wcpay_request:fail,于是找到代码中调用微信支付的代码段:WeixinJSBridge....

发表评论    

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。