1

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>
Mr. ED
  • 11,163
  • 10
  • 51
  • 114
courage
  • 95
  • 2
  • 11
  • This will help you get more information into the problem and will help us help you. http://stackoverflow.com/questions/1053424/how-do-i-get-php-errors-to-display – Goose Mar 03 '17 at 22:11
  • This may help you http://stackoverflow.com/questions/28958745/uploading-a-file-in-codeigniter-php-using-ajax-and-jquery – Mr. ED Mar 03 '17 at 22:24
  • `echo json_encode($somedata)` – Mr. ED Mar 03 '17 at 22:30
  • are you experiencing this problem in your local environment (localhost on your PC) or are you uploading to a live server. If latter, is it a shared server? – Vickel Mar 03 '17 at 22:57
  • 1) Just on the php error reprting. Check if there something kind of error. 2) Or you can check with apache and php logs. – Ashish Tiwari Mar 04 '17 at 04:09
  • INTERNAL SERVER ERROR means your upload_image() function is breaking due to some error, check response in browser's console (if using chrome: go to Network tab and click on url from your ajax call shown in list) and click on response tab from there, it will show you the error generated by codeigniter.) – Vipin Kr. Singh Mar 04 '17 at 10:59
  • these codes works very well with wamp but i can't just figure out why its not working in xampp – courage Mar 04 '17 at 14:50
  • i have done most of the things you suggested but then, it refused to upload – courage Mar 04 '17 at 14:52

0 Answers0