1

The one problem which I can not resolve in while, i tried every possible solution but I doesn't go well Well, the problem is when I want to upload image, i select image from my comp and when I click submit button, image is not upload, and it's show me defaule image which i put in a case that user doesn't select image (noimage.jpg) Function for upload image start in create() method

Posts Controller

<?php


class Posts extends CI_Controller{

    public function __construct(){
        parent::__construct();
        $this->load->database();    
        $this->load->library('upload');

    }

    public function index($offset = 0){



        $config['base_url'] = base_url() . 'posts/index/';
        $config['total_rows'] = $this->db->count_all('posts');
        $config['per_page'] = 3;
        $config['uri_segment'] = 3;
        $config['attributes'] = array('class' => 'pagination-link');

        $this->pagination->initialize($config);

        $data['posts']= $this->Posts_model->get_posts(FALSE,$config['per_page'],$offset);

        $this->load->view('templates/header');
        $this->load->view('posts/index',$data);
        $this->load->view('templates/footer');



    }


    public function view($mjestoOdredista=NULL){


        $data['posts'] = $this->Posts_model->get_posts($mjestoOdredista);
        $post_id = $data['posts']['id'];
        $data['comments'] = $this->comment_model->get_comments($post_id);

        if(empty($data['posts'])){
            show_404();
        }
        $data['id'] =$data['posts'];

        $this->load->view('templates/header');
        $this->load->view('posts/view',$data);
        $this->load->view('templates/footer');


    }


    public function create(){
        //check if user is logged in
        if(!$this->session->userdata('logged_in')){
            redirect('users/login');
        }

       $this->form_validation->set_rules('mjestoPolaska', 'Mjesto Polaska', 'required');
       $this->form_validation->set_rules('mjestoOdredista', 'Mjesto Odredista', 'required');
       $this->form_validation->set_rules('datumPolaska', 'Datum Polaska', 'required');
       $this->form_validation->set_rules('datumPovratka', 'Datum Povratka', 'required');
       $this->form_validation->set_rules('cijena', 'Cijena', 'required');
       $this->form_validation->set_rules('brojMjesta', 'Broj mjesta', 'required');
       $this->form_validation->set_rules('opis', 'Opis', 'required');


        $data['title'] ='Create Posts';
        $data['categories'] = $this->Posts_model->get_categories();

        if($this->form_validation->run()===FALSE){

            $this->load->view('templates/header');
            $this->load->view('posts/create',$data);
            $this->load->view('templates/footer');

        }else {

            //upload image
            $config['upload_path'] = './assets/images/posts';
            $config['allowed_types'] = 'gif|jpg|png';
            $config['max_size'] = '2048';
            $config['max_width'] = '100';
            $config['max_height'] = '100';

            $this->load->library('upload',$config);


            if(!$this->upload->do_upload('userfile')){
                $errors = array('error'=>$this->upload->display_errors());
                $post_image='noimage.jpg';
            }else{
                $data = array('upload_data'=>$this->upload->data());
                $post_image = $_FILES['userfile']['name'];
            }
            $this->Posts_model->create_post($post_image);
            $this->session->set_flashdata('post_creted', 'You post has been created') ;
            redirect('posts');
        }

    }

    public function delete($id){

        if(!$this->session->userdata('logged_in')){
            redirect('users/login');
        }


        $this->Posts_model->delete_post($id);
        $this->session->set_flashdata('post_deleted', 'You post has been deleted  ') ;
        redirect('posts');
    }



    public function edit($mjestoOdredista){
        if(!$this->session->userdata('logged_in')){
            redirect('users/login');
        }

        $data['posts']= $this->Posts_model->get_posts($mjestoOdredista);

        //Check if user is logged in
        if($this->session->userdata('user_id') != $this->Posts_model->get_posts($mjestoOdredista)['user_id']){
            redirect('posts');
        }

        $data['categories'] = $this->Posts_model->get_categories();

        if(empty($data['posts'])){
            show_404();
        }

        $data['title'] = 'Edit Post';
        $this->load->view('templates/header');
        $this->load->view('posts/edit',$data);
        $this->load->view('templates/footer');
    }


    public function update(){
        if(!$this->session->userdata('logged_in')){
            redirect('users/login');
        }

        $this->Posts_model->update_post();
        $this->session->set_flashdata('post_updated', 'You post has been updated ') ;
        redirect('posts');

    }

    }

?>

Posts_model

<?php


class Posts_Model extends CI_Model{

    public function __construct(){
        $this->load->database();

    }

    function get_posts($mjestoOdredista=FALSE, $limit = FALSE,$offset = FALSE){
        if($limit){
            $this->db->limit($limit,$offset);
        }
        if($mjestoOdredista === FALSE){

            $this->db->order_by('posts.id','DESC');
            $this->db->join('categories','categories.id = posts.category_id');
            $query=$this->db->get('posts');
            return $query->result_array();
        }

        $query=$this->db->get_where('posts', array('mjestoOdredista' => $mjestoOdredista));


        return $query->row_array();
    }

    //Kreiranje post
    public function create_post($post_image){
        $mjestoPolaska = url_title($this->input->post('title'));
        $data=array(
                'mjestoPolaska' => $this->input->post('mjestoPolaska'),
                'mjestoOdredista' => $this->input ->post('mjestoOdredista'),
                'datumPolaska' => $this->input ->post('datumPolaska'),
                'datumPovratka' => $this->input ->post('datumPovratka'),
                'brojMjesta' => $this->input ->post('brojMjesta'),
                'cijena' => $this->input ->post('cijena'),
                'opis' => $this->input ->post('opis'),
                'category_id'=>$this->input->post('category_id'),
                'user_id' =>$this->session->userdata('user_id'),
                'post_image'=>$post_image

        );

        return $this->db->insert('posts',$data);
    }

    //Brisanje posta
    public function delete_post($id){
        $this->db->where('id',$id);
        $this->db->delete('posts');
        return true;
    }


    //editovanje posta
    public function update_post(){
        $mjestoOdredista = url_title($this->input->post('title'));
        $data=array(
                'category_id' => $this->input->post('category_id'),
                'mjestoPolaska' =>   $this->input->post('mjestoPolaska'),
                'mjestoOdredista' => $this->input->post('mjestoOdredista'),
                'datumPolaska' =>    $this->input ->post('datumPolaska'),
                'datumPovratka' =>   $this->input ->post('datumPovratka'),
                'cijena' =>          $this->input ->post('cijena'),
                'brojMjesta' =>      $this->input ->post('brojMjesta'),
                'opis' =>            $this->input ->post('opis'),

        );
        $this->db->where('id', $this->input->post('id'));
        return $this->db->update('posts', $data);

    }

    public function get_categories(){
        $this->db->order_by('name');
        $query = $this->db->get('categories');
        return $query->result_array();
    }

    public function get_posts_by_category($category_id){

        $this->db->order_by('posts.id','DESC');

        $this->db->join('categories','categories.id = posts.category_id');
        $query=$this->db->get_where('posts',array('category_id'=>   $category_id));
        return $query->result_array();

    }



}



?>

Create View

<h2><?= $title;?></h2>
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<link rel="stylesheet" href="/resources/demos/style.css">
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>

<script>
  $( function() {
        $("#datepicker").datepicker({
            dateFormat: 'dd-mm-yy'
        });
        $("#datepicker1").datepicker({
            dateFormat: 'dd-mm-yy'
        }); 
  });
  </script>


<?php echo form_open_multipart('posts/create/');?>
<?php echo validation_errors();?>

    <div class="row">
        <div class="col-md-4 col-md-offset-4">

                <div class="form-group">
                    <label>Mjesto Polaska</label>
                    <input type="text" class="form-control" name="mjestoPolaska" placeholder="Mjesto Polaska">
                </div>


                <div class="form-group">
                    <label>Mjesto Odredista</label>
                    <input type="text" class="form-control" name="mjestoOdredista" placeholder="Mjesto Odredista">
                </div>

                <div class="form-group">
                    <label>Datum Polaska</label>
                     <input type="date" id="datepicker" class="form-control" name="datumPolaska" placeholder ="Datum Polaska" >
                </div>

                <div class="form-group">
                    <label>Datum Povratka</label>
                     <input type="date" id="datepicker1" class="form-control" name="datumPovratka" placeholder="Datum Povratka">
                </div>



                <div class="form-group">
                <label>Cijena</label>
                <input type="text" class="form-control" name="cijena" placeholder="Cijena">
                </div>

                <div class="form-group">
              <label for="select">Broj slobodnih mjesta</label>
                  <select class="form-control" id="select" name="brojMjesta">
                    <option>1</option>
                    <option>2</option>
                    <option>3</option>
                    <option>4</option>
                  </select>
                </div>


            <div class="form-group">
            <label for="kategorije">Kategorija</label>
             <?php 
             echo '<select class="form-control" id="kategorije" name="category_id">';
             foreach($categories as $category) :
                echo '<option value="' . $category['id'] . '">' . $category["name"] . '</option>';
             endforeach;
             echo '</select>';
             ?>
            </div>

            <div class="form-group">
             <label>Postavi sliku:</label>
             <p><b>samo oni koji nude prevoz nek postave sliku svojeg vozila</b></p>
             <input type="file" name="userfile" size="20">
             </div>

             <div class="form-group">
              <label>Opis:</label>
              <textarea class="form-control" rows="5" id="comment" name="opis"></textarea>
            </div>
                <button type="submit" class="btn btn-primary btn-block">Submit</button>

            </div>
    </div>
4
  • Which os you are using Commented Sep 12, 2017 at 0:26
  • Windows 10, I hope it's not problem about os :) Commented Sep 12, 2017 at 6:59
  • upload path problem will be there because window uses '\' and linux uses / so. by the way am updated code and wrote comment Commented Sep 12, 2017 at 7:09
  • Yeah, I see the code. I just check it, but I get error:Call to undefined function ImageUpload() Commented Sep 12, 2017 at 7:11

2 Answers 2

2

if store data without image then

public function create() {
    //check if user is logged in
    if (!$this->session->userdata('logged_in')) {
      redirect('users/login');
    }

    $this->form_validation->set_rules('mjestoPolaska', 'Mjesto Polaska', 'required');
    $this->form_validation->set_rules('mjestoOdredista', 'Mjesto Odredista', 'required');
    $this->form_validation->set_rules('datumPolaska', 'Datum Polaska', 'required');
    $this->form_validation->set_rules('datumPovratka', 'Datum Povratka', 'required');
    $this->form_validation->set_rules('cijena', 'Cijena', 'required');
    $this->form_validation->set_rules('brojMjesta', 'Broj mjesta', 'required');
    $this->form_validation->set_rules('opis', 'Opis', 'required');


    $data['title'] = 'Create Posts';
    $data['categories'] = $this->Posts_model->get_categories();

    if ($this->form_validation->run() === FALSE) {

      $this->session->set_flashdata("error", validation_errors());
      $this->load->view('templates/header');
      $this->load->view('posts/create', $data);
      $this->load->view('templates/footer');
    } else {
      $post_image='';
      if (!empty($_FILES['userfile']['name'])) {
        $path = 'assets/images/posts/';
        $post_image = $this->ImageUpload($path, 'userfile', '');
        if (!is_array($post_image)) {

          $this->session->set_flashdata('error', $post_image);
          redirect(site_url() . 'posts/create');
        }
        $post_image = $post_image['file_name'];
      }


      $this->Posts_model->create_post($post_image);
      $this->session->set_flashdata('post_creted', 'You post has been created');
      redirect('posts');
    }
  }
Sign up to request clarification or add additional context in comments.

Comments

2

Try This

Replace your create function with this code

    public function create()
    {
    //check if user is logged in
    if (!$this->session->userdata('logged_in')) {
    redirect('users/login');
    }

    $this->form_validation->set_rules('mjestoPolaska', 'Mjesto Polaska', 'required');
    $this->form_validation->set_rules('mjestoOdredista', 'Mjesto Odredista', 'required');
    $this->form_validation->set_rules('datumPolaska', 'Datum Polaska', 'required');
    $this->form_validation->set_rules('datumPovratka', 'Datum Povratka', 'required');
    $this->form_validation->set_rules('cijena', 'Cijena', 'required');
    $this->form_validation->set_rules('brojMjesta', 'Broj mjesta', 'required');
    $this->form_validation->set_rules('opis', 'Opis', 'required');


    $data['title'] = 'Create Posts';
    $data['categories'] = $this->Posts_model->get_categories();

    if ($this->form_validation->run() === FALSE) {

      $this->session->set_flashdata("error", validation_errors());
      $this->load->view('templates/header');
      $this->load->view('posts/create', $data);
      $this->load->view('templates/footer');
    } else {

      if (empty($_FILES['userfile']['name'])) {
        $this->session->set_flashdata('error', "Please select image");
        redirect(site_url() . 'posts/create');
      }

      $path = 'assets/images/posts/';
      $post_image = $this->ImageUpload($path, 'userfile', '');
      if (!is_array($post_image)) {

        $this->session->set_flashdata('error', $post_image);
        redirect(site_url() . 'posts/create');
      }
      $post_image = $post_image['file_name'];

      $this->Posts_model->create_post($post_image);
      $this->session->set_flashdata('post_creted', 'You post has been created');
      redirect('posts');
    }
  }

  public function ImageUpload($upload_path, $uploading_file_name, $file_name = '') {

    $obj = &get_instance();
    $obj->load->library('upload');
    //echo $uploading_file_name.$file_name;
    $file_name = 'image_' . time() . $file_name . "." . pathinfo($_FILES[$uploading_file_name]['name'], PATHINFO_EXTENSION);
    $full_path = FCPATH . $upload_path;
    //       $full_path = $upload_path;// uncomment if you using windows os
    // if folder is not exists then create the folder
    //if (!is_dir($full_path)) {
    // mkdir($full_path, 0775);
    //}

    $config['upload_path'] = $full_path;
    $config['allowed_types'] = 'gif|jpg|png|jpeg|GIF|JPG|PNG|JPEG';
    $config['max_size'] = 2048;
    $config['max_width'] = 2048;
    $config['max_height'] = 2048;
    $config['file_name'] = $file_name;

    $obj->upload->initialize($config);

    if (!$obj->upload->do_upload($uploading_file_name)) {
      return $obj->upload->display_errors();
    } else {
      return $obj->upload->data();
    }
  }

And Replace <?php echo validation_errors();?> to <?php echo $this->session->flashdata("error")?>

Or you can use layout library instead of calling header n footer [https://code.tutsplus.com/tutorials/how-to-create-a-layout-manager-with-codeigniter--net-15533]

FCPATH works fine in linux system only so check FCPATH if error come path is wrong

11 Comments

I get this error:Call to undefined function ImageUpload()
@yimy did you used like $this->ImageUpload($path, 'userfile', ''); and did you put ImageUpload function in after create function
Yeah, everything just copy-past and chenged path to FCPATH
Call to undefined function ImageUpload() this error Everything I changed, but again same problem
@yimy can you share the code full controller so i can check
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.