0

I'm trying to upload an image to a file path and insert that path to the database. However, the upload always shows the error You did not select a file to upload ..even though I did.

Here's my VIEW

<form action="<?= site_url('profile/profile_submit')?>" method="post" enctype="multipart/form-data">
       <input type="file" class="image-upload" accept="image/*" name="profilePic" id="profilePic"/>
</form>

CONTROLLER

$config['upload_path'] = 'assets/img/profile_img/';
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$config['overwrite'] = TRUE;
$config['max_size'] = "2048000"; 
$config['max_height'] = "768";
$config['max_width'] = "1024";
$this->load->library('upload', $config);
    if ($this->upload->do_upload('profilePic')){
        $data = $this->upload->data();
            $picture = array(
                'photoPath' => $this->upload->data('full_path').$data['file_name']
            );
    }
    else{
            echo $this->upload->display_errors();
    } 

$this->profile_model->submit_profile($picture);

MODEL

function submit_profile($picture){
        $this->db->insert('tbl_st_picture', $picture);
        }

4 Answers 4

2
public function add() { 
    $this->load->helper('url');
    $config['upload_path'] = "./assets/uploads/";
    $config['allowed_types'] = 'gif|jpeg|png|jpg';
    $config['max_height'] = '1000';
    $config['max_width'] = '2048';
    $config['max_size'] = '2048';
    $this->load->library('upload',$config);
    $this->path = './assets/uploads/';

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

    if (! $this->upload->do_upload('berkas'))
    {
        $error = array('error' => $this->upload->display_errors());
        redirect(base_url().'berita/tambah');
    }else{
        $dataUpload = $this->upload->data();

        $data = array(
            'Judul' => $this->input->post('Judul'),
            'Foto' => $dataUpload['file_name'],
            'Isi' => $this->input->post('Isi')
            );
        $this->load->model('berita_model');
        $result = $this->berita_model->insert('berita',$data);

        if ($result>0) {
            redirect(base_url() .'news');
        } else {
            echo "Gagal";
        }
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

function insert ($db,$data){ $result = $this->db->insert($db,$data); return $result; }
these is my model
1

change your upload code as below. Don't use $this->input->post() for getting file data. Also make sure you have added enctype="multipart/form-data" in form

if ($this->upload->do_upload('profilePic')){
       $data = $this->upload->data();
        $picture = array(
                'photoPath' => $this->upload->data('full_path').$data['file_name'],
        );
    }
    else{
            echo $this->upload->display_errors();
    } 

2 Comments

i'm using helpers , im using this form_open_multipart('profile/profile_submit')
Will you show your updated code and form tag in view?
1

if you want, you can try this.

function file_upload()
{
    $config['upload_path'] = './uploads/';

    $config['allowed_types'] = 'gif|jpg|png';
    $config['max_size'] = '100';
    $config['max_width']  = '1024';
    $config['max_height']  = '768';

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

    if ( ! $this->upload->do_upload())
    {
        $error = array('error' => $this->upload->display_errors());

        $this->load->view('upload_form', $error);
    }
    else
    {
        $data = array('upload_data' => $this->upload->data());

        $this->load->view('upload_success', $data);
    }
}

1 Comment

Create a folder name upload into the root path in your project.
0

This how it should be done:

$this->upload->do_upload('profilePic')

The do_upload function does the necessary posting for you.

4 Comments

a tried $this->upload->do_upload('profilePic') , still the same error. I updated my code above
well I copied and pasted your exact code into my localhost CI and it works fine and the images upload (did you verify if the images are in the folder?). beware if you refresh the controller page with the upload code of course you will get an error as there is no post data then. further for photoPath you just do $data['full_path'] it already contains the filename.
Also note that if assets/img/profile_img/ does not exist it will also fail (although I'm not sure what the error message is). CI doesn't create the upload folder, you have to do that yourself.
assets/img/profile_img/ exist. and I've changed the $data['full_path']. still not uploading tho;

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.