php+ajax上传图片

js:

    var imgarr = [];
    $('.file').on('change',function(e){
       var inp = $(this);
       var up = $(e.currentTarget).parents('li');
        $("#mainForm").ajaxSubmit({
        url : "<{:U('Release/ajaxUpload')}>", // 请求的url  
        type : "post", // 请求方式  
        dataType : "json", // 响应的数据类型  
        async :true, // 异步  
        success: function (data1) { 
            // alert(data1);
            // $('.uploadImg').empty();
           var ele = "\
            <li class='img'>\
                <img src='"+data1+"'>\
                <i class='iconfont icon-delete2' onclick='remove(this)'></i>\
            </li>";
            //$('.imgBox').before(ele);
            up.before(ele)
            $('#mainForm').find('input[name=image]').val(data1);
            imgarr.push(data1);
        },  
        error : function(){  
            alert("数据加载失败!"); 
        }  
         });     


    });

<ul class="imgBox" id="1">
                    <li class="up">
                        <span><i class="iconfont icon-plus-bold"></i></span>
                        <input  type="file" name="img" class="file">
                    </li>
                </ul>

php代码:

    /**
     * 上传图片
     */
    public function ajaxUpload()
    {
        $pic = $_FILES['img'];
        $path_pic = './Public/static/images';
        if (!empty($pic['name'])) {
            $pic_res = uploadFile($pic, $path_pic);

            if ($pic_res['status'] == 1) {

                $this->ajaxReturn(trim($pic_res['dest'], '.'));
            } else {
                $this->ajaxReturn(0);
            }
        } else {
            $this->ajaxReturn(0);
        }
    }

uploadFile函数:

//文件上传
function uploadFile($fileInfo, $path = './public/static/images/', $flag = true, $allowExt = array('jpeg', 'jpg', 'png', 'gif', 'mp3', 'wav', 'wma', ''), $maxSize = 524288000)
{
    //判断错误号
    if ($fileInfo['error'] == 0) {
        //检测上传文件的大小  
        if ($fileInfo['size'] > $maxSize) {
            $res['mes'] = $fileInfo['name'] . '上传文件过大';
        }
        $ext = strtolower(pathinfo($fileInfo['name'], PATHINFO_EXTENSION));
        //检测上传文件的文件类型  
        if (!in_array($ext, $allowExt)) {
            $res['mes'] = $fileInfo['name'] . '非法文件类型';
        }
        //检测是否是真实的图片类型  
        if ($flag) {
            if (!getimagesize($fileInfo['tmp_name'])) {
                $res['mes'] = $fileInfo['name'] . '不是真实图片类型';
            }
        }
        // 检测文件是否是通过HTTP POST上传上来的  
        if (!is_uploaded_file($fileInfo['tmp_name'])) {
            $res['mes'] = $fileInfo['name'] . '文件不是通过HTTP POST方式上传上来的';
        }
        if ($res) return $res; //如果要不显示错误信息的话,用if( @$res ) return $res;

        //$path='./uploads';  
        //如果没有这个文件夹,那么就创建一  
        if (!file_exists($path)) {
            mkdir($path, 0777, true);
            chmod($path, 0777);
        }

        //新文件名唯一  
        $uniName = md5(uniqid(microtime(true), true));
        $destination = $path . '/' . $uniName . '.' . $ext;

        //@符号是为了不让客户看到错误信,也可以删除 
        if (!@move_uploaded_file($fileInfo['tmp_name'], $destination)) {
            $res['mes'] = $fileInfo['name'] . '文件移动失败';
        }
        $res['mes'] = $fileInfo['name'] . '上传成功';
        $res['status'] = 1;
        $res['dest'] = $destination;
        return $res;
    } else {
        //匹配错误信息  
        //注意!错误信息没有5  
        switch ($fileInfo['error']) {
            case 1:
                $res['mes'] = '上传文件超过了PHP配置文件中upload_max_filesize选项的值';
                break;
            case 2:
                $res['mes'] = '超过了HTML表单MAX_FILE_SIZE限制的大小';
                break;
            case 3:
                $res['mes'] = '文件部分被上传';
                break;
            case 4:
                $res['mes'] = '没有选择上传文件';
                break;
            case 6:
                $res['mes'] = '没有找到临时目录';
                break;
            case 7:
                $res['mes'] = '文件写入失败';
                break;
            case 8:
                $res['mes'] = '上传的文件被PHP扩展程序中断';
                break;

        }
        $res['status'] = -1;
        return $res;
    }
js需要引入 jquery-form.js

猜你喜欢

转载自blog.csdn.net/helloworld_dream/article/details/80424099