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);
}
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();
}
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);
}
}
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";
}
}
}
This how it should be done:
$this->upload->do_upload('profilePic')
The do_upload function does the necessary posting for you.