Xweibo微博广场上传图片bug修复

选中图片触发input change事件后会请求到application/controllers/api/weibo/action.mod.php

执行upload_pic方法

function upload_pic()
{
$callback = V(‘g:callback’,’’);
if(!preg_match(‘/^[.a-zA-Z0-9_]+$/‘,$callback)){
$callback = ‘callback’;
}
$redirect = ‘window.location=”‘.W_BASE_URL.’js/blank.html?rand=’.rand(1,PHP_INT_MAX) . ‘“‘;

    if (isset($_FILES\['pic'\])) {
        if ($_FILES\['pic'\]\['error'\]) {
            switch ($_FILES\['pic'\]\['error'\]) {
                case '1':
                    $err = APP::ajaxRst(false, '2010009', 'The uploaded file exceeds the upload\_max\_filesize directive in php.ini', true);
                    break;
                case '2':
                    $err = APP::ajaxRst(false, '2010010', 'The uploaded file exceeds the MAX\_FILE\_SIZE directive that was specified in the HTML form', true);
                    break;
                case '3':
                    $err = APP::ajaxRst(false, '2010011', 'The uploaded file was only partially uploaded', true);
                    break;
                case '4':
                    $err = APP::ajaxRst(false, '2010012', 'No file was uploaded', true);
                    break;
                case '6':
                    $err = APP::ajaxRst(false, '2010013', 'Missing a temporary folder', true);
                    break;
                case '7':
                    $err = APP::ajaxRst(false, '2010014', 'Failed to write file to disk', true);
                    break;
            }
            die("");
        } else {
                $has_pic = false;

    if (isset($\_FILES\['pic'\]) && !empty($\_FILES\['pic'\])) {

        if(!empty($\_FILES\['pic'\]\['tmp\_name'\])){
            $\_pathinfo = GetImageSize($\_FILES\['pic'\]\['tmp_name'\]);
            if(empty($\_pathinfo) || $\_pathinfo\[2\] < 1 || $_pathinfo\[2\] > 16 ){
                $_FILES\['pic'\]\['error'\] = 6666;
            }
        }

        if ($_FILES\['pic'\]\['error'\]) {
            if(IS\_IN\_API_REQUEST){
                APP::ajaxRst(false, '2010014', 'UPLOAD\_ERROR:'. $\_FILES\['pic'\]\['error'\]);
            }else{
                $err = APP::ajaxRst(false, '2010014', 'UPLOAD\_ERROR:'. $\_FILES\['pic'\]\['error'\], true);
                die("");
            }

        }
        $has_pic = true;
    }
        if($has_pic){
            $fileName = 'weibo\_img\_strategy_'. time(). '_'. uniqid('', true);
            $file_obj = APP::ADP('upload');
            $fileName = 'weibo\_img\_strategy_'. time(). '_'. uniqid('', true);
            if (!$file_obj->upload('pic', $fileName, FALSE, 'jpg,jpeg,gif,png')) {
                if(IS\_IN\_API_REQUEST){
                    APP::ajaxRst(false, '2010014', 'strategy\_UPLOAD\_ERROR');
                }else{
                    $err = APP::ajaxRst(false, '2010014', 'strategy\_UPLOAD\_ERROR', true);
                    die("");
                }
            }

            //获取上传文件的信息
            $\_image = $file\_obj->getUploadFileInfo();
            $e\_params\['\_upload\_pic\_url'\] = $_image\['webpath'\];

                /*$desPath=$\_SERVER\["DOCUMENT\_ROOT"\]."/data/".$fileName;
             move\_uploaded\_file($\_FILES\['pic'\]\['tmp\_name'\],$desPath);
             $url = 'http://t.quanjinglian.com/'."data/".$fileName;*/
        }


            $result = DR('xweibo/xwb.uploadPic', '',$_image\['webpath'\]);

            //$result = DR('xweibo/xwb.uploadPic', '', $\_FILES\['pic'\]\['tmp\_name'\]);
        }
    } else {
        die("");
    }

    if (!empty($result\['errno'\])) {
        die("");
    }

    $json = array();
    $result = $result\['rst'\];
    $json\['msg'\] = $_image\['webpath'\];

    die("");
}

$result = DR(‘xweibo/xwb.uploadPic’, ‘’,$_image[‘webpath’]);

在这行会转到application/class/weibo.class.php 执行uploadPic方法,该方法已经被官方废弃,$pic参数即刚刚传的$_image[‘webpath’],一个图片网址,上传在自己的服务器上的。

 function uploadPic($pic)
 {

    $response=array(
        'errno'=>0,
        'err'=>'',
        'rst'=>array('msg'=>$pic)
        );
    //echo $\_GET\['callback'\].'('.json\_encode($response).');';
    return $response;
 }返回正确的JSON格式就好了

然后点击发布转入执行action.mod.php的update方法,此时的传递过来的pic变量是个字符串而不是先前需要处理的$_FIELS[‘pic’] 在线访谈的页面是直接提交到这个update方法的所以对于上传的图片处理还不能去掉也要与现在这个pic变量区别开来 通过下面的方法获取图片网址

$picurl=V(‘p:pic’, ‘’);

if($picurl){
$has_pic=true;
}

$has_pic设为true以便针对有图和无图情况的处理

if (true != $has_pic) {

        /// 调用发布微博api
        $result = DS('xweibo/xwb.update', '', $text);
    } else {

        /// 调用发布图片微博api

        $result = DR('xweibo/xwb.upload', '', $text,$picurl);

因为微博广场发布图片微博最终需要返回JSON而不是JSONP所以后面要额外处理下

if($callback){
die(“”);
}else{

        die(APP::ajaxRst($json, 0, '', true));}