Shabrina Arin
Shabrina Arin

Reputation: 13

How to upload image file in Codeigniter

I can't upload an image file, I don't know the input file and can't detect it. That's why I can't upload a file. This is the controller, I named it desain.php.

defined('BASEPATH') OR exit('No direct script access allowed');

class Desain extends CI_Controller {

public function index()
{
    $query = "select * from result,kategori where result.id_cat=kategori.id_cat";
    $tampil = $this->db->query($query)->result();
    $this->load->view('Admin/data.php', ['tampil' => $tampil]);
}

public function do_upload()
{
        $config['upload_path']      = './asset/upload/';
        $config['allowed_types']    = 'gif|jpg|png';
        $config['max_size']         = '2048000';
        $config['max_width']        = '1024';
        $config['max_height']       = '1024';
        $config['file_name']        = $this->input->post('photo');
        $this->load->library('upload',$config);

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

            redirect('admin/desain');
        }
        else { $data = $this->upload->data($config);
            $data = array('photo'       =>$this->upload->data($config),
                          'nama_desain' =>$this->input->post('nama'),
                          'id_cat'      =>$this->input->post('categori'),
                          'ket'         =>$this->input->post('ket'),
            );
            $this->db->insert('result',$data);
            redirect('admin/desain');
        }
}

}

This is the view:

 <form role="form" action="<?php echo base_url();?>admin/desain/do_upload" method="post" enctype="multipart/form-data">
  <div class="box-body">
    <div class="form-group">
      <label for="exampleInputEmail1">Nama Desain</label>
      <input type="text" class="form-control" name ="nama" id="nama" placeholder="Nama Desain">
    </div>
    <div class="form-group">
      <label for="exampleInputPassword1">Kategori</label>
      <select class="form-control" id="categori" name="categori">
            <option value="">Pilih Kategori</option>
            <option value="1">Vektor</option>
            <option value="2">Editing</option>
            <option value="3">Desain</option>
      </select>
    </div>
    <div class="form-group">
      <label for="exampleInputPassword1">Keterangan Gambar</label>
       <textarea class="form-control" rows="3" name="ket" id="ket" placeholder="Enter ..."></textarea>
    </div>
    <div class="form-group">
      <label for="exampleInputFile">File input</label>
      <input type="file" required="required" id="photo" name="photo" class="form-control">
    </div>

  </div>
  <!-- /.box-body -->

  <div class="box-footer">
    <button type="submit" class="btn btn-primary">Submit</button>
  </div>
</form>

I tried to post this form, but this file input is NULL.

Upvotes: 1

Views: 3565

Answers (4)

shishir sharma
shishir sharma

Reputation: 34

if(!$this->upload->do_upload('photo'))
{
    $error = array ('error' =>$this->upload->display_errors());
    redirect('admin/desain');
} else { 
    $uploadData = $this->upload->data();
    $picture = $uploadData['file_name'];
    $data = array(
        'photo' => $uploadData['file_path'].'upload_images/'. $picture,
        'nama_desain' =>$this->input->post('nama'),
        'id_cat' =>$this->input->post('categori'),
        'ket' =>$this->input->post('ket'),
    );
    $this->db->insert('result',$data);
    redirect('admin/desain');
}

Upvotes: 0

Alex
Alex

Reputation: 9265

You don't need to do this: $config['file_name'] = $this->input->post('photo');

By default the upload class will use the name of the image as the name of the final image. If you wanted to do it this way than $_FILES['photo']['name']; would work (but again, there is no need).

Further you aren't getting the value of the uploaded photo correctly. I'm not sure why you are putting the config array in this function: $this->upload->data($config); but that is not how it supposed to be used.

If you wanted to get the filename for example you could do:

$filename = $this->upload->data('file_name');

OR

$up_data = $this->upload->data();
$filename = $up_data['file_name']

See this for reference: https://www.codeigniter.com/userguide3/libraries/file_uploading.html#CI_Upload::data


Also you are not doing anything with this line: $error = array ('error' =>$this->upload->display_errors());. I suggest setting the error as a flash message before redirecting.

Upvotes: 1

Arif Sajal
Arif Sajal

Reputation: 133

You should catch the file by

$config['file_name'] = $_FILES['name']['photo'];

Than you will be able to get the image file to process.

Before it please check if the field is able to get or not by

if(!empty($_FILES['name']['photo']))

This was perfectly working on my computer. Hope it will be helpful for you.

Upvotes: 2

Danish Ali
Danish Ali

Reputation: 2352

Try this hope it will help you

$config['upload_path'] = './asset/upload/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size']     = 2048000;
$config['max_width'] = 1024;
$config['max_height'] = 1024;
$config['file_name']        = $this->input->post('photo');
$this->load->library('upload', $config);
if (!$this->upload->do_upload($config['file_name'])) 
{
    $error = $this->upload->display_errors();
    //coding...

}
else 
{
    $filename = $this->upload->data();
    $data[$config['file_name']] = $filename['file_name'];
    //coding...
}

Upvotes: 1

Related Questions