0

Hi guys i am trying to upload image in to uploads folder and insert that path into the database when i tried noting is happening.i don't know what is the exact problem when i upload the image it is showing upload image name in URL. like this

http://localhost/pes/Content/editContent?details=download.jpg&uploadimg=upload

Here is my controller:

function do_upload() {
    $config['upload_path'] = '/uploads/';
    $config['allowed_types'] = 'gif|jpg|png';
    $config['max_size'] = '1000';
    $config['max_width'] = '';
    $config['max_height'] = '';
    $config['overwrite'] = TRUE;
    $config['remove_spaces'] = TRUE;

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

    if (!$this->upload->do_upload()) {
        $error = array('error' => $this->upload->display_errors());
        $this->Content_model->insert_images($this->upload->data());
        $this->load->view('template/header');
        $this->load->view('content/content', $error);
        $this->load->view('template/footer');
    } else {
        $this->Content_model->insert_images($this->upload->data());
        $data = array('upload_data' => $this->upload->data());
        $this->load->view('template/header');
        $this->load->view('content/editdescription', $data);
        $this->load->view('template/footer');
    }
}

Here is my model:

function insert_images($image_data = array()){
  $data = array(
      'details' => $image_data['details']
   );
  $this->db->insert('contentdetails', $data);
 }

Here is my view:

<form>
                    <?php echo form_open_multipart('Content/do_upload'); ?>
                    <div class="form-group">
                        <div class="col-sm-9">
                            <input type="file" name="details" size="20" multiple="true" />
                        </div>
                    </div>
                    <br /><br />
                    <button type="submit"  name="uploadimg" value="upload" class="form-control btn btn-main">Upload File</button>

                </form>

Can any one help me what mistake i have done.

Thanks in advance.

10
  • echo the $error var in your view. what does it say? or echo it in your controller Commented Jul 25, 2018 at 7:08
  • yeah when i use echo $error it is showing undefined variable $error Commented Jul 25, 2018 at 7:09
  • <?php echo $error;?> if i pass like this its giving me error(Undefined variable: error) can you please tell me how can i pass? Commented Jul 25, 2018 at 7:11
  • then you must be entering the 2nd condition where error isn't defined which is bad design. but you are aware that 1) codeigniter doesn't out of the box support multiple images in one upload right 2) $image_data['details'] isn't defined anywhere Commented Jul 25, 2018 at 7:12
  • no its not defined Commented Jul 25, 2018 at 7:13

2 Answers 2

1
function do_upload() {
    $config['upload_path'] = './uploads/'; // make folder if doesn't exist; ci won't do this for you
    $config['allowed_types'] = 'gif|jpg|png';
    $config['max_size'] = '1000';
    $config['max_width'] = '';
    $config['max_height'] = '';
    $config['overwrite'] = TRUE;
    $config['remove_spaces'] = TRUE;

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

    $data['error'] = '';

    if (!$this->upload->do_upload('details')) {
        $data['error'] = $this->upload->display_errors();
        // an error occurred, why would you want to insert nothing?
        //$this->Content_model->insert_images($this->upload->data());
        $this->load->view('template/header');
        $this->load->view('content/content', $data); // echo $error in view
        $this->load->view('template/footer');
    } else {
        $this->Content_model->insert_images($this->upload->data());
        $data['upload_data'] = $this->upload->data();
        $this->load->view('template/header');
        $this->load->view('content/editdescription', $data);
        $this->load->view('template/footer');
    }
}


function insert_images($image_data = array()){
  $data = array(
      'details' => $image_data['full_path']
   );
  $this->db->insert('contentdetails', $data);
 }

HTML:

<input type="file" name="details" size="20" />

Remove:

<form>

Multiple files: https://www.google.com/search?q=codeigniter+multiple+file+upload&rlz=1C1CHBF_enUS777US777&oq=CODEIGNITER+MULTI&aqs=chrome.2.69i57j69i60j0l4.2783j0j4&sourceid=chrome&ie=UTF-8

Sign up to request clarification or add additional context in comments.

7 Comments

The upload path does not appear to be valid.
i have already uploads folder in my xampp/ htdocs/pes/uploads
there was one more change.... ./uploads/ notice the dot... same as in the examples in the docs...
yes i have used ./uploads/ like that but still gettign same error
because ./ refers to your doc root which is xampp/htdocs ... so if you want it to be pes/uploads you have to write ./pes/uploads/ or just make a folder in your doc root called uploads which would be the htdocs folder
|
1

The issue is here:

<form>
<?php echo form_open_multipart('Content/do_upload'); ?>

it is creating two different forms, one by html tag and another one by CI html form helper. So remove the outer one and try again.

Reference

6 Comments

getting this error: The upload path does not appear to be valid.
Check the path you are using, that exist and have file write permission
hi i have created new folder and gave full permission but still getting same problem
@hi i ahve one doubt may i ask
Yes, go ahead and ask.
|

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.