0

I require help with Image upload code with CodeIgniter. Please refer to my code below

I did try File Upload class, but I am failing on using it on already created code for adding new Products.

This is my Form

<form action="<?php echo base_url().'index.php/admin/Welcome/add_new_product'; ?>" method="POST">
  <section class="content">
    <div class="row">
      <div class="col-md-12">
        <div class="box box-info">
          <div class="box-header">
            <h3 class="box-title">Product description
              <small></small>
            </h3>


            <h1><input type="textarea" name="pd_title" placeholder="Enter Title Here" style="width:100%;"></h1>
          </div>
          <!-- /.box-header -->
          <div class="box-body pad">

                  <textarea id="editor1" name="pd_short_desc" rows="10" cols="80">
                                          This is my textarea to be replaced with CKEditor.
                  </textarea>
                </div>
                  <div class="box-body pad">
                  <label for="Image">Upload Image</label>
                  <input type="file" name="pd_img" value="" style="padding:10px;">
                </div>

                  <div class="box-body pad">
                  <h3 class="box-title">Description to right of Image</h3>
                  <textarea id="editor2" name="pd_info_right" rows="10" cols="80">
                                          This is my textarea to be replaced with CKEditor.
                  </textarea>
                </div>
                  <div class="box-body pad">
                  <h3>Product Complete Description</h3>
                  <textarea id="editor3" name="pd_desc" rows="10" cols="80">
                                          This is my textarea to be replaced with CKEditor.
                  </textarea>
                </div>

                <input type="submit" name="submit" class="btn  btn-info btn-raised ink-reaction">
            </form>

This is my Controller

public function add_new_product()
    {
        $p = $this->model_one->add_new_product();
        redirect('admin/Welcome/view_products');
    }

This Model should insert data in mysqli db table

public function add_new_product()
{
  $pd_title = $_POST['pd_title'];
  $pd_short_desc = $_POST['pd_short_desc'];
  $pd_image = $_POST['pd_image'];
  $pd_info_right = $_POST['pd_info_right'];
  $pd_desc = $_POST['pd_desc'];

  $sql = $this->db->query("INSERT INTO products (pd_title, pd_short_desc, pd_image, pd_info_right, pd_desc)
  VALUES('$pd_title','$pd_short_desc', '$pd_image','$pd_info_right','$pd_desc')");

  return "yes";
}

When submit this form, Image needs to upload in uploads folder, as well insert into products table in mysqli db.

Riyaz
  • 109
  • 1
  • 1
  • 13

1 Answers1

0

Do some changes

View

Add enctype="multipart/form-data" in the <form> tag

<form action="<?php echo base_url().'index.php/admin/Welcome/add_new_product'; ?>" method="POST" enctype="multipart/form-data">

Controller

Get POST values in the controller

public function add_new_product()
{
    $data = array('pd_title' => $_POST['pd_title'], 'pd_short_desc' => $_POST['pd_short_desc'], 'pd_info_right' => $_POST['pd_info_right'], 'pd_desc' => $_POST['pd_desc']);

    $config['upload_path'] = FCPATH.'uploads/'; 
    $config['allowed_types'] = 'gif|jpg|png'; 
    $config['max_size'] = 100; 
    $config['max_width'] = 1024; 
    $config['max_height'] = 768;

    if(isset($_FILES['pd_img']['name'])){
       $this->load->library('upload', $config);
       if(!$this->upload->do_upload('pd_img')){ 
           print_r($this->upload->display_errors());
       }else{
           $data['pd_image'] = $this->upload->data('file_name');
       }
    }

    $this->model_one->add_new_product($data);
    redirect('admin/Welcome/view_products');
}

Model

public function add_new_product($data)
{
    return $this->db->insert('products', $data);
}
Danish Ali
  • 2,354
  • 3
  • 15
  • 26
  • if(isset($_FILES['pd_img']['name'])) { //Image uploading... $config['upload_path'] = './uploads/'; $config['allowed_types'] = 'gif|jpg|png'; $config['max_size'] = 100; $config['max_width'] = 1024; $config['max_height'] = 768; $this->upload->do_upload('pd_image'); } No Success, image is uploading in uploads folder but not getting aded to sqli db – Riyaz May 30 '19 at 06:09
  • Try this in the controller `if( !$this->upload->do_upload('pd_image')){ print_r($this->upload_display_errors());}` to get any error message – Danish Ali May 30 '19 at 06:11
  • public function add_new_product() { $data = array('pd_title' => $_POST['pd_title'], 'pd_short_desc' => $_POST['pd_short_desc'], 'pd_info_right' => $_POST['pd_info_right'], 'pd_desc' => $_POST['pd_desc']); if( !$this->upload->do_upload('pd_image')){ print_r($this->upload_display_errors());} $this->model_one->add_new_product($data); redirect('admin/Welcome/view_product'); } Is this correct – Riyaz May 30 '19 at 06:19
  • No Success - A PHP Error was encountered Severity: Notice Message: Undefined property: Welcome::$upload Filename: admin/Welcome.php Line Number: 211 Backtrace: File: D:\xampnew\htdocs\cms\application\controllers\admin\Welcome.php Line: 211 Function: _error_handler File: D:\xampnew\htdocs\cms\index.php Line: 315 Function: require_once – Riyaz May 30 '19 at 06:29
  • Let me complete this. First, tell me what is the project directory structure? I mean the targeted folder? – Danish Ali May 30 '19 at 06:35
  • danish ali uploads folder – Riyaz May 30 '19 at 06:42
  • Fatal error: Call to undefined method Welcome::upload_display_errors() in D:\xampnew\htdocs\cms\application\controllers\admin\Welcome.php on line 220 A PHP Error was encountered Severity: Error Message: Call to undefined method Welcome::upload_display_errors() Filename: admin/Welcome.php Line Number: 220 Backtrace: – Riyaz May 30 '19 at 06:52
  • @Riyaz it's `$this->upload->display_errors()`. It was typing issue from my side – Danish Ali May 30 '19 at 06:55
  • Danish Ali and Alex I thank you for helping me fixing this, Danish your code did work, and now the Image does get added in uploads folder as well in the mysqli db. Thank you again. – Riyaz May 30 '19 at 07:01
  • Now as the images are uploading in upload folder and Mysqli, how can images be displayed when viewed online by public. – Riyaz May 30 '19 at 07:19
  • First, get the product record from DB and use image name in the `` tag like `` – Danish Ali May 30 '19 at 07:32