当前位置:首页 > 程序心得 > 正文内容

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

admin9年前 (2016-09-19)程序心得1670
 /**
     * 上传图片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上传到远程服务器


分享到:

扫描二维码推送至手机访问。

版权声明:本文由云河空间发布,如需转载请注明出处。

本文链接:https://yuyunhe.cn/index.php/post/188.html

分享给朋友:

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

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

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

SVN Skipped 'xxx' -- Node remains in conflict 错误的解决办法

svn提交发现错误#cd /home/svn/app/# svn upUpdating '.':Skipped 'xxx' -- Node remains in conflictAt revision 1054.Summary of conflicts: ...

RSA密钥的生成与配置(支付宝公私密钥可用)

RSA密钥的生成与配置(支付宝公私密钥可用)

RSA密钥的生成与配置openssl下载地址http://dldx.csdn.net/fd.php?i=20313208579480&s=ac2e809e168f7d5b8bf1515d3d6b1aa4,或者官方下载通过openssl工具生成RSA的公钥和私钥(opnssl工具可在互联网中下...

JQ 获取验证码倒计时方法

JQ 获取验证码倒计时方法

html: <a href="javascript:;" class="weui_btn weui_btn_mini weui_btn_default"  id="show-not...

使用FastClick消除IOS点击延时提高程序的运行效率

FastClick是一个非常方便的库,在移动浏览器上发生介于轻敲及点击之间的指令时,能够让你摆脱300毫秒的延迟。FastClick可以让你的应用程序更加灵敏迅捷。支持各种移动浏览器,比如Safari、Chrome、Opera等。FastClick 是一个简单,易于使用的JS库用于消除在移动浏览器上...

ios5的safari浏览器的电话号码识别功能的禁用

更详细的apple官方文档: https://developer.apple.com/library/safari/#featuredarticles/iPhoneURLScheme_Reference/Articles/PhoneLinks.html在编写 HTML 时,有一个 meta...

发表评论

访客

看不清,换一张

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