php,js实现手机图片上传功能(thinkphp,mobile.js)
admin
2023-06-28 14:04:09
0
  1. 页面内容显示和js加载

  2. php代码编写


    //检测并上传图片

        public function attach_upload()

        {

                $id = $_REQUEST['id'] ? $_REQUEST['id'] : "";

                $attach_access_key = $_REQUEST['attach_access_key'] ? $_REQUEST['attach_access_key'] : "";

            import("@.ORG.UploadPic");

            $upload = new UploadPic();

            $savepath = "data/upload/weixinpic/".date("Y-m-d")."/";

            if(!file_exists($savepath)){

                $this->createDir($savepath);

            }

            $upload->initialize(array(

                    'allowed_types' => 'jpg,jpeg,png,gif',

                    'upload_path' => $savepath,

                    'is_p_w_picpath' => FALSE,

                    'max_size' => 10240

            ));

            if (isset($_REQUEST['qqfile']))

            {

                    $upload->do_upload($_REQUEST['qqfile'], true);

            }

            else if (isset($_FILES['qqfile']))

            {

                    $upload->do_upload('qqfile');

            }

            else

            {

                    return false;

            }

            if ($upload->get_error())

            {

                    switch ($upload->get_error())

                    {

                            default:

                              die("{'error':'错误代码: " .                                             $upload->get_error() . "'}");

                            break;

                            case 'upload_invalid_filetype':

                                    die("{'error':'文件类型无效'}");

                            break;

                            case 'upload_invalid_filesize':

                             die("{'error':'文件尺寸过大, 最大允                            许尺寸为 10240 KB'}");

                            break;

                    }

            }

            if (! $upload_data = $upload->data())

            {

                    die("{'error':'上传失败, 请与管理员联系'}");

            }

            if ($upload_data['is_p_w_picpath'] == 1)

            {

                    $config['p_w_upload_thumbnail'] = array(

                            'min' => array(

                                    "w" => 170, 

                                    "h" => 110

                            ), 

                            'square' => array(

                                   "w" => 90, 

                                    "h" => 90

                            )

                    );

                    import("@.ORG.Images");

                    $p_w_picpath = new Images();

                    foreach($config['p_w_upload_thumbnail'] AS $key => $val)

                    {

                            $thumb_file[$key] = $upload_data['file_path'] . $val['w'] . 'x' . $val['h'] . '_' . basename($upload_data['full_path']);

                            $p_w_picpath->initialize(array(

                                    'quality' => 90,

                                    'source_p_w_picpath' => $upload_data['full_path'],

                                    'new_p_w_picpath' => $thumb_file[$key],

                                    'width' => $val['w'],

                                    'height' => $val['h']

                            ))->resize();

                    }

            }

            $ptem['ue_id'] = $ueid;

            $add = array();

            $add['file_name'] = $upload_data['orig_name'];

            $add['access_key'] = $attach_access_key;

            $add['add_time'] = time();

            $ainfo = M('attach')->where($ptem)->find();

            if(!empty($ainfo)){

                $maxid = M('attach')->where($ptem)->max("que_sort");

                $add['que_sort'] = $maxid + 1;

            }else{

                $add['que_sort'] = 0; 

            }

            if(false == strpos($upload_data['full_path'],"http://")){

                 $filepath = "http://".$_SERVER['HTTP_HOST']."/".$savepath.basename($upload_data['full_path']);// 文件真实路径

            }else{

                 $filepath = $savepath.basename($upload_data['full_path']);

            }

            $add['file_location'] = basename($upload_data['full_path']);

            $add['user_que_content'] =  $filepath;

            $attach_id = M("attach")->add($add);

            unset($add);

            $output = array(

                        'success' => true,

                        'delete_url' => '?m=upload&a=remove_attach&attach_id=' . base64_encode(encode_hash(array(

                        'attach_id' => $attach_id, 

                        'access_key' => $attach_access_key

                    ))),

                    'attach_id' => $attach_id,

                    'attach_tag' => 'attach'

            );

            //获取内容

            $attachinfo = M("attach")->where("id = " . intval($attach_id))->find();    

            if (!empty($attachinfo))

            {

                    $data = $this->parse_attach_data(array($attachinfo),                                 'square');

                    $attach_info = $data[intval($attach_id)];

            }

            if ($attach_info['thumb'])

            {

                    $output['thumb'] = $attach_info['thumb'];

            }

            echo htmlspecialchars(json_encode($output), ENT_NOQUOTES);

    }

    

    //删除图片附件

    public function remove_attach()

    {

            $attach_id = $_REQUEST['attach_id'] ? trim($_REQUEST['attach_id']) : '';

            //判断id是否为空

            if(!$attach_id){

                echo str_replace(array("\r", "\n", "\t"), '',                                json_encode(array('rsm' => "图片id不存在无法删除",'errno' =>                    0,'err' => "图片id不存在无法删除")));

                exit;

            }

            if ($attach_info = decode_hash(base64_decode($attach_id)))

            {

                    $id = $attach_info['attach_id'];

                    $access_key = $attach_info['access_key'];

                    //判断图片是否存在

                    if (! $attach = M("attach")->where("id = " . intval($id) . "                       AND access_key = '" . $access_key . "'")->find())

                    {

                           echo str_replace(array("\r", "\n", "\t"), '', json_encode(array('rsm' => "图片id不存在无法删除",'errno' => 0,'err' => "图片id不存在无法删除")));

                           exit;

                    }

                    //删除图片

                    M("attach")->where("id = " . intval($id) . " AND access_key = '" . $access_key . "'")->delete();

                    //删除图片文件

                    $attach_dir = "data/upload/weixinpic/".date("Y-m-d")."/";

                    $config['p_w_upload_thumbnail'] = array(

                            'min' => array(

                                    "w" => 170, 

                                    "h" => 110

                            ), 

                            'square' => array(

                                    "w" => 90, 

                                    "h" => 90

                            )

                    );

                    foreach($config['p_w_upload_thumbnail'] AS $key => $val)

                    {

                            @unlink($attach_dir . $val['w'] . 'x' . $val['h'] . '_' . $attach['file_location']);

                    }

                    @unlink($attach_dir . $attach['file_location']);

            }

            echo str_replace(array("\r", "\n", "\t"), '', json_encode(array('rsm' => null,'errno' => 1,'err' => null)));

            exit;

    }


    

    //处理并解析附件数据

    public function parse_attach_data($attach, $size = null)

    {

            if (!$attach)

            {

                return false;

            }

            $config['p_w_upload_thumbnail'] = array(

                    'min' => array(

                            "w" => 170, 

                            "h" => 110

                    ), 

                    'square' => array(

                            "w" => 90, 

                            "h" => 90

                    )

            );

            $attach_url = "data/upload/weixinpic/".date("Y-m-d")."/";

            foreach ($attach as $key => $data)

            {

                    $attach_list[$data['id']] = array(

                            'id' => $data['id'], 

                            'is_p_w_picpath' => 1, 

                            'file_name' => $data['file_name'], 

                            'access_key' => $data['access_key'], 

                            'p_w_upload' => $data['user_que_content'],

                    );

                    if ($size)

                    {

                        $attach_list[$data['id']]['thumb'] = $attach_url . '/' . $config['p_w_upload_thumbnail'][$size]['w'] . 'x' . $config['p_w_upload_thumbnail'][$size]['h'] . '_' . $data['file_location'];

                    }

            }

            return $attach_list;

    }

4.效果图

php,js实现手机图片上传功能(thinkphp,mobile.js)

php,js实现手机图片上传功能(thinkphp,mobile.js)

php,js实现手机图片上传功能(thinkphp,mobile.js)


附件:http://down.51cto.com/data/2365571

相关内容

热门资讯

绿色算力全栈AI平台在呼和浩特...   新华社呼和浩特5月30日电(记者侯维轶)30日,绿色算力全栈AI平台——内蒙古词元交易平台在内蒙...
北京太空智算研究院在北京亦庄成... 红星资本局5月30日消息,近日,北京太空智算研究院在北京经济技术开发区(简称“北京经开区”,又称“北...
第二届西部医学科技创新学术大会... 封面新闻记者 邱添 2026年5月30日,在第十个全国科技工作者日来临之际,一场汇聚医学前沿智慧与创...
地博光电取得存储自动化料库专利... 国家知识产权局信息显示,昆山地博光电材料有限公司取得一项名为“一种存储自动化料库”的专利,授权公告号...
香港政务司司长谈黎家盈:香港由... 据凤凰卫视报道,首位来自香港的载荷专家黎家盈早前随神舟二十三号载人飞船升空。香港政务司司长陈国基表示...
亚美尼亚大选在即,俄方突然召回... 【文/观察者网 阮佳琪】当地时间周五(5月29日),在哈萨克斯坦首都阿斯塔纳,亚美尼亚由副总理格里戈...
财政部在香港发行60亿元人民币... 新华社北京5月30日电 《中国证券报》30日刊发文章《财政部在香港发行60亿元人民币绿色主权债券》。...
原创 追... 2026年5月30日是全国科技工作者日。在吉林省长春市王大珩故居前,这位中国光学事业奠基人的新雕像揭...
京东618独家首发欧姆龙血糖尿... 当前,国民居家慢病管理意识持续增强,血糖、尿酸等健康指标的常态化监测,早已成为家庭健康管理的刚需。5...
华为申请天线旋转调整波束角度专... 国家知识产权局信息显示,华为技术有限公司申请一项名为“天线、通信设备以及通信系统”的专利,公开号CN...