doujunchi1238 2017-03-03 22:06
浏览 37

codeigniter ajax图片上传

I was following a tutorial that teaches how to to upload image with ajax and CodeIgniter. But i kind of run into INTERNAL SERVER ERROR each time i tried to run the code.i have checked the folder where i suppose the image get uploaded to but it was empty. Tried all i could to get this thing working but it appears i can't. So, i would like someone check these codes out for me.

CONTROLLER

    public function upload_image()
    {
        $data['title'] = 'Codeigniter Image upload';
        $this->load->view('image_upload', $data);
    }

    public function ajax_upload()
    {
        if (isset($_FILES["image_file"]["name"])) {
            $config['upload_path']  = './uploads/';
            $config['allowed_types'] = 'jpeg|gif|jpg|png';
            $this->load->library('upload', $config);
            if (!$this->upload->do_upload('image_file')) {
                echo '<p class="text-danger">'.$this->upload->display_errors().'</p>';
            } else {
                $data = $this->upload->data();
                 $config['image_library'] = 'gd2';
                 $config['source_image'] = './uploads/'.$data["file_name"];
                $config['create_thumb'] = FALSE;
                $config['maintain_ratio'] = FALSE;
                $config['quality'] = '60%';
                $config['width'] = 200;  
                $config['height'] = 200;
                $config['new_image'] = './uploads/'.$data["file_name"];
                // load image library
                $this->load->library('image_lib', $config);
                $this->image_lib->resize();

                $img_data = array(
                    'name' => $data['file_name']
                    );
                $this->main_model->insert_img($img_data);
                echo $this->main_model->fetch_img();
            }
        } 
    }

Views file that contains the form

<html lang="en">
<head>
    <meta charset="utf-8">
    <title>myschools | <?php echo $title; ?></title>
    <link rel="stylesheet" href="<?php echo base_url('assets/css/bootstrap.min.css'); ?>">
    <!-- <link rel="stylesheet" href="<?php //echo base_url('assets/css/custom.css'); ?>"> -->
</head>
<body>
  <div class="container">
    <h3 align="center"><?php echo $title; ?></h3><br>

    <form class="form-inline" role="form" align="center" id="upload_form" method="post" enctype="multipart/form-data">

      <div class="form-group">
        <label class="sr-only" for="image_file">Select Image</label>
        <input type="file" class="form-control" id="image_file" name="image_file">
      </div>
      <button type="submit" class="btn btn-info">Upload</button>
  </form>
  <br>
  <br>
  <br>
  <div id="uploaded_image"></div>
  </div>
<script src="<?php echo base_url('assets/js/jquery.min.js'); ?>"></script>
<script src="<?php echo base_url('assets/js/bootstrap.min.js'); ?>"></script>

</body>
</html>
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 有人能看一下我宿舍管理系统的报修功能该怎么改啊?链表那里总是越界
    • ¥15 cs loadimage运行不了,easyx也下了,没有用
    • ¥15 r包runway详细安装教程
    • ¥15 Html中读取Json文件中数据并制作表格
    • ¥15 谁有RH342练习环境
    • ¥15 STM32F407 DMA中断问题
    • ¥15 uniapp连接阿里云无法发布消息和订阅
    • ¥25 麦当劳点餐系统代码纠错
    • ¥15 轮班监督委员会问题。
    • ¥20 关于变压器的具体案例分析