使用codeigniter框架

时间:2016-03-22 12:57:57

标签: jquery ajax codeigniter

我尝试使用codeigniter框架上传带有ajax功能的图片。 $ this-> upload-> do_upload(' file'),以及如何获取图像文件的所有详细信息,例如在codeigniter中我们使用$ _FILE()。

控制器

class Upload extends CI_Controller{
#code   
public function index(){        
    $this->load->view('upload');
}
public function do_upload(){       
   if(!empty($_FILES)){
        $config = array(
            'upload_path' => './uploads/',
            'allow_types' => 'gif|jpg|jpeg|png|JPG|JPEG|PNG|GIF',
            'max_size'  => '1024',
            'max_width' => '1024',
            'max_height' => '1000',
            'encrypt_name' => TRUE,
            'overwrite' => TRUE
        );
        $this->load->library('upload', $config);
        //$this->upload->initialize($config);

        echo "test1 :<br/>";
        print_r($this->upload->do_upload('file'));
        echo "test2 : <br/>";

        if(!$this->upload->do_upload('file')){
            echo "unable to upload images";
        }else{
            $data = $this->upload->data();
            $this->thumb($data);
            $file = array(
                'filename' => $data['file_name']
            );
            $this->upload_model->imageupload($file);
            $data = array('upload_data'=>$this->upload->data());
        }
        //redirect(site_url('upload'));
   }
}
}

查看                                                                                                                                                                                                                                       图片上传                                                                                                                                    

脚本

<script type="text/javascript">

     $(document).ready(function(){


        $('#userfile').change(function(){

           //$('.img_pre').attr('src','<?php echo base_url(); ?>uploads/ProgressBar.gif');

           $('#btn_upload').submit();

        });

        var inputFile = $('input[name=userfile]');

        var uploadURL = '<?= site_url('upload/do_upload')?>';

        $('#btn_upload').on('click', function(e){

           e.preventDefault();

           $('.img_pre').attr('src','<?php echo base_url(); ?>uploads/ProgressBar.gif');

           var fileToupload = inputFile[0]. files[0];

           if (fileToupload != 'undefined') {

              //code

              var formData = new FormData();

              formData.append('file', fileToupload);

              $.ajax({

                 type: 'POST',

                 url : uploadURL,

                 data: formData,

                 //dataType: 'json',

                 async: false,

                 processData:false,

                 contentType:false,

                 cache: false,

                 success: function(data){


                 }

              });

           }else{

               alert("Error :"+data);

           }

        });

     });

    </script>

1 个答案:

答案 0 :(得分:0)

如手册所述

$这 - &GT; upload-&GT;数据()

这是一个辅助函数,它返回一个数组,其中包含与您上传的文件相关的所有数据。这是数组原型:

Array
(
    [file_name]    => mypic.jpg
    [file_type]    => image/jpeg
    [file_path]    => /path/to/your/upload/
    [full_path]    => /path/to/your/upload/jpg.jpg
    [raw_name]     => mypic
    [orig_name]    => mypic.jpg
    [client_name]  => mypic.jpg
    [file_ext]     => .jpg
    [file_size]    => 22.2
    [is_image]     => 1
    [image_width]  => 800
    [image_height] => 600
    [image_type]   => jpeg
    [image_size_str] => width="800" height="200"
)

在您的情况下,$ data将保存该信息