Buat artikel dengan Codeigniter 3.x + fitur upload gambar yang tidak terupload

Halo para sedulur semua,

Saya kebetulan saat ini sedang memiliki project yang cukup krusial buat saya sendiri. Project website informasi sih, lebih tepatnya.

Belakangan ini, hal yang cukup menjebak saya adalah ketika saya mencoba untuk membuat artikel dengan menambahkan gambar, namun ternyata gambar yang saya ambil tidak terupload (secara otomatis pula tidak masuk record database MySQL).

Kode pertama : news_model



public function set_news()
        {
        $this->load->library("upload");
        $this->load->helper('url');
        $slug = url_title($this->input->post('title'),'dash',TRUE);
        $config['upload_path'] = './img/media/';
            $config['allowed_types'] = 'gif|jpg|png';
            $this->upload->initialize($config);
            $this->upload->do_upload('pictures');
            $image_data = $this->upload->data();
            $pictures = $image_data['file_name'];
            $data = array(
                'title' => $this->input->post('title'),
                'text' => $this->input->post('text'),
                'category' => $this->input->post('category'),
                'tags' => $this->input->post('tags'),
                'caption' => $this->input->post('caption'),
                'slug' => $slug,
                'pictures' => $pictures
            );
    return $this->db->insert('news', $data);
        }

Kode kedua : controller news



public function create()
        {
            $this->load->helper('form');
            $this->load->library('form_validation');
            $data['title'] = 'Add Article';
            $this->form_validation->set_rules('title', 'Title', 'required');
            $this->form_validation->set_rules('text', 'Text', 'required');
            $this->form_validation->set_rules('caption', 'Caption', 'required');
            $this->form_validation->set_rules('category', 'Category', 'required');
            $this->form_validation->set_rules('tags', 'Tags', 'required');
            $this->form_validation->set_rules('Pictures', 'Pictures', 'required');

            if ($this->form_validation->run() === FALSE)
            {
                $this->load->view('konten/templates/header', $data);
                $this->load->view('news/create', $data);
                $this->load->view('konten/templates/footer', $data);
            }
            else
            {
                $this->load->model('news_model');
                $this->news_model->set_news();
                redirect('news', 'refresh');

        }
    }

Kode ketiga : view create

 <?php echo validation_errors(); ?>

<?php echo form_open('news/create'); ?>

    <label>Title</label>
    <input type="text" name="title" size="30" class="input-buat"/><br><br>

    <label>Text</label>
    <textarea name="text" id="text" size="30" class="input-buat"></textarea><br><br>

    <label>Caption</label>
    <textarea name="caption" id="caption" size="30" class="input-buat"></textarea><br><br>

    <label>Category</label>
    <input type="text" name="category" size="30" class="input-buat"><br><br>

    <label>Tags</label>
    <input type="text" name="tags" size="30" class="input-buat"><br><br>

    <label for="pictures">Pictures</label>
    <input type="file" name="pictures" size="30" class="input-buat"><br><br>

    <input type="submit" class="btn-edited" name="submit" value="Kirim" />

</form>

Untuk posisi folder imagenya ada di root file -> /img/media.

Saya sudah mencoba otak atik ini dan itu, tapi masih belum ketemu alasan kenapa file gambar (masuk dari input pictures) tidak bisa terupload dan muncul di view artikelnya.

Terima kasih.

avatar rinaldoaldo92
@rinaldoaldo92

6 Kontribusi 0 Poin

Diperbarui 6 tahun yang lalu

2 Jawaban:

Agan mau upload file, tapi attribut enctype -nya ngga ada, makanya filenya ga keupload,

Ganti

<?php echo form_open();  ?>

dengan

 <?php echo form_open_multipart(); ?>

untuk lebih jelasnya cek dokumentasinya sini https://www.codeigniter.com/userguide3/helpers/form_helper.html

avatar ahanafi
@ahanafi

815 Kontribusi 552 Poin

Dipost 6 tahun yang lalu

Oke, saya sudah mengubah ke form_open_multipart dan sudah juga berhasil upload, baik mengarah ke database saya maupun ke folder tempat si gambar ini seharunsnya.

Namun, yang jadi masalah saya gagal menampilkan ke sisi view artikelnya.

Terlampir kodenya.

 <div id="konten">
<nav aria-label="breadcrumb">
	<ol class="breadcrumb bg-dark">
		<li class="breadcrumb-item" aria-current="page"><a href="<?php echo base_url("news");?>">Beranda</a></li>
		<li class="breadcrumb-item active" aria-current="page"><?php echo $news_item['title']; ?></li>
	</ol>
</nav>

<?php
echo '<div id="meta-konten">';
echo '<h2>'.$news_item['title'].'</h2>';
echo 'Diterbitkan pada <p>'.$news_item['datetime'].'</p>';
echo 'Diubah pada <p>'.$news_item['datetime'].'</p>';
echo 'Penulis <img src="" alt="avatar"> <p>'.$news_item['submitted'].'</p><hr>';
echo '<blockquote><i>'.$news_item['caption'].'</i></blockquote>';
echo '</div>';
echo '<div id="isi-konten">';
echo '<p>'.$news_item['pictures'].'</p>';
echo '<p>'.$news_item['text'].'</p>';
echo '</div>';
?>
</div>

Kalau yang ini view list artikelnya.

<div id="konten">
<div id="list-konten">
<?php foreach ($news as $news_item): ?>
		<img class="img-fluid rounded gambar-pendukung" src="<?php echo $news_item['pictures']; ?>" alt="gambar-pendukung">
		<div class="judul-caption">
        <h5><?php echo $news_item['title']; ?></h5>
        <p><?php echo $news_item['caption']; ?></p>
        <a href="<?php echo base_url('news/'.$news_item['slug']); ?>"><button class="btn-edited">Selengkapnya</button></a>
    </div>
<?php endforeach; ?>
</div>
</div>

Selain itu, apakah kode yang saya tulis termasuk aman dan rapi? Jika ada feedback buat hal ini, dengan senang hati saya terima.

Sekedar info, saya menggunakan Bootstrap 4.0

avatar rinaldoaldo92
@rinaldoaldo92

6 Kontribusi 0 Poin

Dipost 6 tahun yang lalu

Login untuk ikut Jawaban