upload image codeigniter

saya punya kendala di upload image jadi si image yang saya insert ke database itu tidak muncul dan tidak ada pesan error nya jadi saya kurang paham dimana error nya. mohon bantuan nya barangkali ada yang sudah pernah upload image di ci. dan ini script saya.

file controller :

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

class C_item extends CI_Controller {

	protected $access = array('Admin','Pengguna');

	public function __construct()
	{
		parent::__construct();
		$this->load->model('M_item');
		$this->load->library('form_validation');
		$this->load->helper(array('form', 'url'));
	}

	//input item
	public function index()
	{
			$data = array(
			'tabel_item' => $this->M_item->tampil_item(),
		);

		$this->load->view('cover/header');
		$this->load->view('item/tampil_item', $data);
		$this->load->view('cover/footer');
	}

	//tampil tabel
	public function add_item()
	{
		$this->form_validation->set_rules('nama_item', 'Nama Item', 'required');
			$this->form_validation->set_rules('harga', 'Harga', 'required');
			$this->form_validation->set_rules('berat', 'Berat', 'required');
			$this->form_validation->set_rules('status', 'Status', 'required');
			$this->form_validation->set_rules('deskripsi', 'Deskripsi', 'required');

			$nama = 'file_'.time();
			$config['upload_path']	 = './assets/image/uploads/';
			$config['allowed_types'] = 'gif|jpg|png';
			$config['max_size']		 = '1000';
			$config['max_width']	 = '5000';
			$config['max_height']	 = '5000';
			$config['file_name']	 = $nama;

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

		if ($this->form_validation->run() == TRUE && $this->upload->do_upload('foto'))
			{
				$data = array(
				'nama_item'	=> $this->input->post('nama_item',TRUE),
				'harga'		=> $this->input->post('harga',TRUE),
				'berat'		=> $this->input->post('berat',TRUE),
				'status'	=> $this->input->post('status',TRUE),
				'deskripsi'	=> $this->input->post('deskripsi',TRUE),
				'image'		=> $gambar['file_name']
			);

			$this->M_item->insert_item($data);
			redirect(site_url('C_item'));
			}
			else
			{
				$data['foto'] = '';

				if (! $this->upload->do_upload('foto'))
				{
					$data['foto'] = $this->upload->display_errors();
				}
				$this->load->view('cover/header');
				$this->load->view('item/form_item', $data);
				$this->load->view('cover/footer');
		    }
	}

	//update item
	public function get_data($id)
	{
		$data['new_item'] = $this->M_item->get_data($id);

		$this->load->view('cover/header');
		$this->load->view('item/update_item', $data);
		$this->load->view('cover/footer');
	}

	public function update_item()
	{
		$id = $this->input->post('id');

		$data = array(
			'nama_item'	=> $this->input->post('nama_item',TRUE),
			'harga'		=> $this->input->post('harga',TRUE),
			'berat'		=> $this->input->post('berat',TRUE),
			'status'	=> $this->input->post('status',TRUE),
			'deskripsi'	=> $this->input->post('deskripsi',TRUE),
		);

		$this->M_item->update_item($data, $id);
		redirect(site_url('C_item/item'));
	}

	//delete Item
	public function delete_item($id)
	{
		$this->M_item->delete_item($id);
		redirect(site_url('C_item/item'));
	}

}

/* End of file C_item.php */
/* Location: ./application/controllers/C_item.php */

ini file model nya :

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

class M_item extends CI_Model {

	function tampil_item()
	{
		return $this->db->get('t_item')->result();
	}

	//insert item
	function insert_item($data)
	{
		$this->db->insert('t_item', $data);
	}

	//update item
	function get_data($id)
	{
		return $this->db->get_where('t_item', array('id_item' => $id))->row();
	}

	function update_item($data, $id)
	{
		$this->db->where('id_item', $id);
		$this->db->update('t_item', $data);

	}

	//delete item
	function delete_item($id)
	{
		$this->db->where('id_item', $id);
		$this->db->delete('t_item');
	}

}

/* End of file M_item.php */
/* Location: ./application/models/M_item.php */

dan ini file insert nya :

 <!-- Right side column. Contains the navbar and content of the page -->
        <aside class="right-side">
            <!-- Content Header (Page header) -->
            <section class="content-header">
                <!--section starts-->
                <h1>Add Item</h1>

                <ol class="breadcrumb">
                    <li>
                        <a href="index.html">
                            <i class="livicon" data-name="home" data-size="14" data-loop="true"></i>
                            Dashboard
                        </a>
                    </li>
                </ol>
                <?php echo validation_errors(); ?>
                <?php echo form_open('C_item/index'); ?>
            </section>

            <!--section ends-->
<!--md-6 ends-->
                    <div class="col-md-6">
                        <!--md-6 starts-->
                        <!--form control starts-->
                        <div class="panel panel-success" id="hidepanel6">
                            <div class="panel-heading">
                                <h3 class="panel-title">
                                    <i class="livicon" data-name="share" data-size="16" data-loop="true" data-c="#fff" data-hc="white"></i>
                                    form validation
                                </h3>
                                <span class="pull-right">
                                    <i class="glyphicon glyphicon-chevron-up clickable"></i>
                                    <i class="glyphicon glyphicon-remove removepanel clickable"></i>
                                </span>
                            </div>
                            <div class="panel-body">
                                <form action="<?= site_url('C_item/add_item') ?>" method="POST" role="form"  enctype="multipart/form-data">
                                    <div class="form-group">
                                        <label>Nama Item</label>
                                        <input type="text" name="nama_item" class="form-control"  placeholder="Nama Item">
                                    </div>
                                    <div class="form-group">
                                        <label>Harga Item</label>
                                        <input type="text" name="harga" class="form-control"  placeholder="Harga Item">
                                    </div>
                                    <div class="form-group">
                                        <label>Berat Item</label>
                                        <input type="text" name="berat" class="form-control"  placeholder="Berat Item">
                                    </div>
                                    <div class="form-group">
                                        <label>Status</label>
                                        <div class="col-md-4 col-sm-12">
                                            <select name="status" class="form-control">
                                            <option value="">--Pilih Status--</option>
                                            <option value="1">Aktif</option>
                                            <option value="2">Tidak Aktif</option>
                                        </select>
                                        </div>
                                    </div>
                                    <div class="form-group">
                                        <label>Deskripsi</label>
                                        <textarea class="form-control" name="deskripsi" rows="3"></textarea>
                                    </div>
                                    <div class="form-group">
                                        <div class="fileinput fileinput-new" data-provides="fileinput">
                                            <div class="fileinput-new thumbnail" style="width: 200px; height: 150px;">
                                                <img data-src="holder.js/100%x100%" alt="..."></div>
                                            <div class="fileinput-preview fileinput-exists thumbnail" style="max-width: 200px; max-height: 150px;"></div>
                                            <div>
                                                <span class="btn btn-default btn-file">
                                                    <span class="fileinput-new">Select image</span>
                                                    <span class="fileinput-exists">Change</span>
                                                    <input type="file" name="foto"></span>
                                                <a href="#" class="btn btn-default fileinput-exists" data-dismiss="fileinput">Remove</a>
                                            </div>
                                        </div>
                                    </div>
                                    <button type="submit" value="submit" class="btn btn-responsive btn-default">Submit Button</button>
                                    <button type="reset" class="btn btn-responsive btn-default">Reset Button</button>
                                    <a href="<?= site_url('C_item/item'); ?>" type="button" class="btn btn-responsive btn-default">Back</a>
                                </form>
                            </div>
                        </div>
                    </div>
                    <!--md-6 ends-->
                </aside>
avatar Aditiyafadillah33
@Aditiyafadillah33

48 Kontribusi 3 Poin

Diperbarui 6 tahun yang lalu

2 Jawaban:

$gambar['file_name'] sepertinya belum didefinisikan, coba tambah ini sebelum array:

$gambar = $this->upload->data();

Controllernya jadi gini:

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

class C_item extends CI_Controller {

	protected $access = array('Admin','Pengguna');

	public function __construct()
	{
		parent::__construct();
		$this->load->model('M_item');
		$this->load->library('form_validation');
		$this->load->helper(array('form', 'url'));
	}

	//input item
	public function index()
	{
			$data = array(
			'tabel_item' => $this->M_item->tampil_item(),
		);

		$this->load->view('cover/header');
		$this->load->view('item/tampil_item', $data);
		$this->load->view('cover/footer');
	}

	//tampil tabel
	public function add_item()
	{
		$this->form_validation->set_rules('nama_item', 'Nama Item', 'required');
			$this->form_validation->set_rules('harga', 'Harga', 'required');
			$this->form_validation->set_rules('berat', 'Berat', 'required');
			$this->form_validation->set_rules('status', 'Status', 'required');
			$this->form_validation->set_rules('deskripsi', 'Deskripsi', 'required');

			$nama = 'file_'.time();
			$config['upload_path']	 = './assets/image/uploads/';
			$config['allowed_types'] = 'gif|jpg|png';
			$config['max_size']		 = '1000';
			$config['max_width']	 = '5000';
			$config['max_height']	 = '5000';
			$config['file_name']	 = $nama;

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

		if ($this->form_validation->run() == TRUE && $this->upload->do_upload('foto'))
			{
				$gambar = $this->upload->data();

				$data = array(
				'nama_item'	=> $this->input->post('nama_item',TRUE),
				'harga'		=> $this->input->post('harga',TRUE),
				'berat'		=> $this->input->post('berat',TRUE),
				'status'	=> $this->input->post('status',TRUE),
				'deskripsi'	=> $this->input->post('deskripsi',TRUE),
				'image'		=> $gambar['file_name']
			);

			$this->M_item->insert_item($data);
			redirect(site_url('C_item'));
			}
			else
			{
				$data['foto'] = '';

				if (! $this->upload->do_upload('foto'))
				{
					$data['foto'] = $this->upload->display_errors();
				}
				$this->load->view('cover/header');
				$this->load->view('item/form_item', $data);
				$this->load->view('cover/footer');
		    }
	}

	//update item
	public function get_data($id)
	{
		$data['new_item'] = $this->M_item->get_data($id);

		$this->load->view('cover/header');
		$this->load->view('item/update_item', $data);
		$this->load->view('cover/footer');
	}

	public function update_item()
	{
		$id = $this->input->post('id');

		$data = array(
			'nama_item'	=> $this->input->post('nama_item',TRUE),
			'harga'		=> $this->input->post('harga',TRUE),
			'berat'		=> $this->input->post('berat',TRUE),
			'status'	=> $this->input->post('status',TRUE),
			'deskripsi'	=> $this->input->post('deskripsi',TRUE),
		);

		$this->M_item->update_item($data, $id);
		redirect(site_url('C_item/item'));
	}

	//delete Item
	public function delete_item($id)
	{
		$this->M_item->delete_item($id);
		redirect(site_url('C_item/item'));
	}

}

/* End of file C_item.php */
/* Location: ./application/controllers/C_item.php */
avatar ruswan
@ruswan

31 Kontribusi 21 Poin

Dipost 6 tahun yang lalu

solved bang

avatar Aditiyafadillah33
@Aditiyafadillah33

48 Kontribusi 3 Poin

Dipost 6 tahun yang lalu

Login untuk ikut Jawaban