登录  /  注册
使用tp3.2和mbUploadify.js上传图片的代码示例
little bottle
发布: 2023-04-06 08:40:01
转载
1231人浏览过

本文主要讲述了使用tp3.2和mbUploadify.js来实现上传图片,具有一定的参考价值,分享给大家,有兴趣的朋友了解一下哦!

HTML:

<p class="form-group">
    <label class="col-sm-1 control-label no-padding-right" for="form-field-4"> 图片: </label>
    <p class="col-sm-9">
        <input type="file" name="files" id="imgfile" multiple style="display:none;" onchange = "imgpath.value=this.value" >
        <input type="textfield" id="imgpath" style="border: 0px;outline:none;cursor: pointer;width:100px;display:none;">
        <input type="button" class="btn btn-white btn-info btn-sm" value="点击上传图片" onclick="imgfile.click()">
        <p class="space-4"></p>
        <p id="img-data" class="img-data">
            <if condition="$data['savepath'] neq ''">
                <span class="uploadimg">
                    <img src="{$data['savepath']}" style="max-width: 300px;">
                    <input type="hidden" name="img" value="{$data['img']}">
                    <a class="remove-uploadimg" title="删除">✕</a>
                </span>
            </if>
        </p>
        <p class="space-4"></p>
        <p id="imgError" class="msg"></p>
    </p></p>
登录后复制

CSS:

<style>
    .remove-uploadimg{ cursor:pointer;}
    .uploadimg{
        display: inline-block;
        position: relative;
    }
    .uploadimg .remove-uploadimg{
        position: absolute;
        font-size: 20px;
        top:-10px;
        right: -6px;
    }
    .remove-uploadimg{
        width:30px;
        height:30px;
        background-color:#ccc;
        border-radius:50%;
        color:red;
        text-align:center;
    }
    .remove-uploadimg:hover{
        text-decoration: none;
    }</style>
登录后复制

JS:

<script src="__PUBLIC__/js/mbUploadify.js"></script>
<script>    var upload1 = new mbUploadify({
        file: document.getElementById('imgfile'),        /*ajax 上传地址*/
        url: "{:U('Upload/mbUploadImg')}",        //上传进度
        progress: function(){
            $('#imgpath').show();
            $('#imgpath').val('上传中...');
        },        /*上传失败*/
        error: function(file, msg){
            document.getElementById('imgError').innerHTML = msg;
        },        /*ajax上传成功*/
        uploadSuccess: function(res){
            $('#imgpath').hide();
            $('#imgpath').val('');            var data = JSON.parse(res);
            document.getElementById('img-data').innerHTML = '<span class="uploadimg">' +
                    '<img src="'+ data.savepath +'" style="max-width: 300px;">' +
                    '<input type="hidden" name="img" value="'+data.id+'">'+
                    '<a class="remove-uploadimg" title="删除">✕</a>' +
                    '</span>';
        }
    });
    $('body').on('click','.remove-uploadimg',function(){
        $(this).parents('.uploadimg').remove();
    })</script>
登录后复制

PHP:

public function mbUploadImg(){    $upload = new Upload(); // 实例化上传类
    $upload->maxSize = 5242880 ; // 设置附件上传大小
    $upload->exts = array('jpg', 'gif', 'png', 'jpeg'); // 设置附件上传类型
    $upload->rootPath =  './Public/';    // 上传文件
    $info = $upload->uploadOne($_FILES['files']);    if($info) {        // 上传成功
        $data['name'] = $info['name'];        $data['ext'] = $info['ext'];        $data['type'] = $info['type'];        $data['savename'] = $info['savename'];        $data['savepath'] = "/Public/".$info['savepath'].$info['savename'];        $imgId = M('upload_img')->add($data);        if($imgId){            $resData['code'] = 200;            $resData['msg'] = '成功';            $resData['id'] = $imgId;            $resData['name'] = $data['name'];            $resData['savepath'] = $data['savepath'];            echo json_encode($resData);            return;
        }
    }    // 上传错误提示错误信息
    return $this->ajaxReturn(['code'=>400,'msg'=>$upload->getError()]);
}
登录后复制

相关教程:

PHP视频教程

HTML视频教程

CSS视频教程

js视频教程

以上就是使用tp3.2和mbUploadify.js上传图片的代码示例的详细内容,更多请关注php中文网其它相关文章!

相关标签:
来源:博客园网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 技术文章
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2023 //m.sbmmt.com/ All Rights Reserved | 苏州跃动光标网络科技有限公司 | 苏ICP备2020058653号-1

 | 本站CDN由 数掘科技 提供

登录PHP中文网,和优秀的人一起学习!
全站2000+教程免费学