Jquery Multi文件上传使用PHP FrameWork

时间:2014-02-01 02:29:51

标签: jquery ajax

我想使用Jquery Ajax Form Upload和PHP Framework Codeigniter上传一些图像。 在输入表单中有一些带有类属性“upload_button”的“上传按钮”。 但是错误发生了。 怎么了? 编程代码如下。 请帮帮我!

的Javascript

$(function(){
    $('.upload_button').click(function() {
        //index Number
        var num=$('.upload_button').index(this);
        var Formdata=new FormData();
        var files=$("input[name=image" + num + "]")[0].files;
        $.each(files, function(i, file){
            Formdata.append("userfile[]", file);
        });
        Formdata.append("key",num);
        //Send FormData 
        $.ajax({
            type: 'POST',
            url: "/upload_images",
            contentType: false,
            processData: false,
            dataType: 'json',
            data: Formdata,
            error: function() {
                console.log('error');
                alert("foo");
            },
            success: function(data, status){
                console.log('success');
            }
        });
        // false
        //return false;
    });
});

PHP(笨)

public function index(){
    $ret = array();
    //Array(Post)
    $post_array=array();
    $post_array=$this->input->post();
    //Array(Session)
    $session_array=array();
    $session_array=$this->session->all_userdata();
    if(!array_key_exists("member_id",$session_array)){
        return FALSE;
    }
    if(!array_key_exists("key",$post_array)){
        return FALSE;
    }
    $image_name="iamge".$post_array["key"];
    //Upload
    $array=  $this->upload_image($image_name);
    if(array_key_exists("error",$array))
        $ret["error"]=$array["error"];
    }else{
        $ret["image"]=$array["upload_data"]["file_name"];
    }
    echo json_encode($ret);
}

//Upload Image
private function upload_image($field){
    $config['upload_path']='./image';
    $config['allowed_types']='jpg|jpeg';
    $config['max_size']='100';
    $config['max_width']='280';
    $config['max_height']='210';
    $config['file_name']=time();
    $config['overwrite']=FALSE;
    $config['encrypt_name']=TRUE;
    $this->load->library('upload');
    $this->upload->initialize($config);
    if(!$this->upload->do_upload($field)){
        $display_errors=array('error' => $this->upload->display_errors('<span class="em">','</span>'));
        return $display_errors;
    }else{
        $data=array('upload_data' => $this->upload->data());
        //Adjust Image Size
        $this->resize_image($data["upload_data"]["file_name"]);
        return $data["upload_data"]["file_name"];
    }
}

//Fix Image Size
private function resize_image($file){
`enter code here`    $config['image_library']='gd';
    //$config['source_image']='C:\xampp\htdocs\image\\'.$file;
    $config['source_image']='/var/www/vhosts/test.com/httpdocs/image/'.$file;
    //$config['create_thumb']=TRUE;
    $config['maintain_ratio']=TRUE;
    $config['quality']="60";
    $config['master_dim']='height';
    $config['width']="200";
    $config['height']="150";
    $this->load->library('image_lib');
    $this->image_lib->initialize($config);
    //$this->image_lib->resize();
    if ( ! $this->image_lib->resize()){
        echo $this->image_lib->display_errors();
    }
}

0 个答案:

没有答案