so i have this function in my controller, but i don't know how to implement it in my view, i manage to upload the image into the database but the image that i upload didnt show up in any folder only in database and also i want to rename the image into the date:month:year(timestamp), what do i miss?
Controller
public function do_upload()
{
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '500';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$this->load->library('upload', $config);
$this->upload->initialize($config);
if ( ! $this->upload->do_upload('foto'))
{
$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);
$file = $data['upload_data']['full_path'];
}
}
View
<div class="form-group">
<label>Foto</label>
<input type="file" action="<?php echo site_url('admin/barang/do_upload');?>" name="foto" id="foto" class="form-control">
</div>
<button type="submit" class="btn btn-primary" style="width:100%;">Tambah</button>
EDIT
so i've been dying to do this stuff, do some research and read a lot of articles but none seems work, as far as i get my view was under form action="admin/barang/insert"
, and that makes my 2nd form get ignore, in any way how do we call 2 action in the same form?
the problem was fixed, instead making a new function do_upload()
in Controller, i put what inside do_upload()
function into my insert()
function, so my insert()
function have something like this
//photo
$photoName = gmdate("d-m-y-H-i-s", time()+3600*7).".jpg";
$config['upload_path'] = './assets/img/barang';
$config['allowed_types'] = 'gif||jpg||png';
$config['max_size'] = '2048000';
$config['file_name'] = $photoName;
$this->load->library('upload',$config);
if($this->upload->do_upload('userfile')){
$upload = 1;
}
else{
$upload = 2;
}
and then put some if else statement that if upload success, the data will be update otherwise no
Try this.
On Your view :
<?php echo form_open_multipart('admin/barang/do_upload');?>
<div class="form-group">
<label>Foto</label>
<?php echo form_upload('foto'); ?>
</div>
<button type="submit" class="btn btn-primary" style="width:100%;">Tambah</button>
<?php echo form_close(); ?>
On Your controller :
public function do_upload()
{
$config['upload_path'] = './uploads/';//this is the folder where the image is uploaded
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '500';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$config['file_name'] = 'enter new file name here';//rename file here
$this->load->library('upload', $config);
$this->upload->initialize($config);
if ( ! $this->upload->do_upload('foto'))
{
$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);
$file = $data['upload_data']['full_path'];
}
}
Hope this will help you.