Bagaimana agar form upload gambar berhasil

Permisi teman-teman, ada yang saya mau tanyakan perihal coding codeigniter. Jadi saya membuat form upload foto, tetapi setelah dilakukan tes upload, foto-foto yang telah dipilih tidak terlihat di database maupun folder local padahal tidak ada error yang tampak.

Berikut saya cantumkan codingnya:


<!-- Content -->
<section class="content-area p-y-1">
    <div class="container-fluid">
        <h4><?= ucfirst($page) ?> Pole</h4>
        <div class="pull-right ">
            <a href="<?= site_url('pole') ?>">
                <button type="button" class="btn btn-warning pull-left m-r-0-5 label-left waves-effect waves-light">
                    <pole class="btn-label"><i class="ti-arrow-left"></i></pole>
                    Back
                </button>
            </a>
        </div>
        <ol class="breadcrumb no-bg m-b-1">
            <li class="breadcrumb-item"><a href="<?= site_url() ?>">Dashboard</a></li>
            <li class="breadcrumb-item active">New Pole</li>
        </ol>
    </div>

    <!-- Main Content -->
    <div class="container-fluid">
        <?php $this->view('messages') ?>
        <div class="card card-block">
            <div class="row">
                <div class="col-md-12">
                    <div class="pull-right ">
                        <a href="<?= site_url('pole') ?>">Cancel</a>
                    </div>
                    <h5>Input the new pole below</h5>
                    <text>Can input once or major</text>
                    <br>
                    <div class="box-body">
                        <?php echo form_open_multipart('pole/process') ?>
                            <div class="row">
                                <div class="col-md-4"><br>
                                    <div class="control-group after-add-more">
                                        <div class="form-group">
                                            <label><strong>Pole Name*</strong></label>
                                            <div class="pull-right add-more">
                                                <button type="button" class="btn btn-success btn-sm label-left b-a-0 waves-effect waves-light">
                                                    <span class="btn-label"><i class="ti-plus"></i></span>
                                                    Add
                                                </button>
                                            </div>
                                            <input type="text" class="form-control" value="<?= $row->pole_name ?>" name="pole_name" data-mask="aaa-99-999" placeholder="AAA-99-999" style='text-transform:uppercase' required>
                                        </div>
                                        <div class="form-group">
                                            <label><strong>Coordinate</strong></label>
                                            <input type="text" class="form-control" value="" name="coordinate" placeholder="-9.999999, 999.999999" data-mask="-9.999999, 999.999999">
                                        </div>
                                        <div class="form-group">
                                            <label><strong>Pole Photo</strong></label>
                                            <input type="file" name="photo" id="input-file-max-fs" class="form-control dropify" data-max-file-size="2M" />
                                        </div>
                                        <div class="form-group">
                                            <label><strong>Description</strong></label>
                                            <textarea class="form-control" name="description" placeholder="Fill the Description"><?= $row->description ?></textarea>
                                        </div>
                                    </div>
                                    <br>
                                    <div class="form-group">
                                        <button type="submit" name="<?= $page ?>" class="btn btn-success btn-flat">
                                            <i class="fa fa-paper-plane"></i> Save</button>
                                        <button type="reset" class="btn btn-flat">Reset</button>
                                    </div>
                                    <!-- <div class="copy invisible">
                                        <div class="control-group"><br>
                                            <div class="form-group">
                                                <label><strong>pole Name</strong></label>
                                                <div class="pull-right remove">
                                                <button type="button" class="btn btn-danger btn-sm label-left b-a-0 waves-effect waves-light">
                                                    <span class="btn-label"><i class="ti-close"></i></span>
                                                    Remove
                                                </button>
                                            </div>
                                                <input type="text" class="form-control" value="<?= $row->pole_name ?>" name="pole_name" placeholder="Fill the pole name">
                                            </div>
                                            <div class="form-group">
                                                <label><strong>Description</strong></label>
                                                <textarea style="resize:none;height:57px;" class="form-control" name="description" placeholder="Fill the Description"><?= $row->description ?></textarea>

                                            </div>
                                        </div>
                                    </div> -->
                                </div>

                                <div class="col-md-8">
                                    <table class="table table-striped table-bordered dataTable display nowrap" style="width:100%" id="table">
                                        <thead>
                                            <tr>
                                                <th>Pole Name</th>
                                                <th>Description</th>
                                                <th>Edit</th>
                                            </tr>
                                        </thead>
                                        <tbody>
                                                <?php $no = 1;
                                                foreach($row_data->result() as $key => $data) { ?>
                                                <tr>
                                                    <td><?=$data->pole_name?></td>
                                                    <td><?=$data->description?></td>
                                                    <td width="50px" align="center">
                                                        <a href="<?=site_url('pole/edit/'.$data->pole_id)?>" class="btn btn-primary btn-xs">
                                                            <i class="fa fa-pencil"></i>
                                                        </a>
                                                        <?php if($this->fungsi->user_login()->level == 1) { ?>
                                                        <a href="<?=site_url('pole/del/'.$data->pole_id)?>" onclick="return confirm('Apakah anda yakin?')" class="btn btn-danger btn-xs">
                                                            <i class="fa fa-trash"></i>
                                                        </a>
                                                        <?php } ?>
                                                    </td>
                                                </tr>
                                            <?php }?>
                                        </tbody>
                                        <tfoot>
                                            <tr>
                                                <th>Pole Name</th>
                                                <th>Description</th>
                                                <th>Edit</th>
                                            </tr>
                                        </tfoot>
                                    </table>
                                </div>
                            </div>
                        <?php echo form_close() ?>
                    </div>
                </div>
            </div>
        </div>
    </div>
</section>

view


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

class pole extends CI_Controller {

    function __construct() {

        parent::__construct();
        check_not_login();
        $this->load->model('pole_centralregion');

    }

    public function index() {

        $pole = new stdClass();
        $pole->pole_id = null;
        $pole->pole_name = null;
        $pole->coordinate = null;
        $pole->description = null;

        $data = array(
            'page' => 'add',
            'row' => $pole,
            'row_data' => $this->pole_centralregion->get(),
        );

        // $i = 0; // untuk loopingnya
        // $a = $this->input->post('pole_name');
        // $b = $this->input->post('description');
        // if ($a[0] !== null)
        // {
        // foreach ($a as $loop)
        // {
        //     $data = array(
        //         'pole_name' => $loop,
        //         'description' => $b[$i],
        //     );

        //     $insert = $this->db->insert('dm_pole', $data);
        //     if ($insert) {
        //     $i++;
        //     }
        // }
        // }

        $this->template->load('template', 'support_manager/pole/pole_data', $data);
    }

    public function edit($id) {

        $query = $this->pole_centralregion->get($id);
        if($query->num_rows() > 0) {
            $pole = $query->row();
            $data = array(
                'page' => 'edit',
                'row' => $pole,
                'row_data' => $this->pole_centralregion->get(),
            );
            $this->template->load('template', 'support_manager/pole/pole_data', $data);
        } else {
            echo "<script>alert('Data tidak ditemukan');";
            echo "window.location='".site_url('user')."';</script>";
        }
    }

    public function process() {

        $post = $this->input->post(null, TRUE);
        if(isset($_POST['add'])) {
            if($this->pole_centralregion->check_id($post['pole_name'])->num_rows() > 0) {
                $this->session->set_flashdata('error', "This $post[pole_name] has been saved with same name, try another name please!");
                redirect('pole');

            } else {
                $config['upload_path']          = './uploads/pole/';
                $config['allowed_types']        = 'gif|jpg|png|jpeg';
                $config['max_size']             = 2048;
                $config['file_name']            = 'item-'.date('ymd').'-'.substr(md5(rand()),0,10);
                $this->load->library('upload', $config);

                if(@$_FILES['photo']['pole_name'] != null) {
                    if($this->upload->do_upload('photo')) {
                        $post['photo'] = $this->upload->data('file_name');
                        $this->pole_centralregion->add($post);

                        if($this->db->affected_rows() > 0) {
                            $this->session->set_flashdata('success', '<strong>Well done!</strong> You successfully add <a href="#" class="alert-link">data</a>.');
                        }
                        redirect('pole');
                    } else {
                        $error = $this->upload->display_errors();
                        $this->session->set_flashdata('error', $error);
                        redirect('pole');
                    }
                } else {
                    $post['photo'] = null;
                    $this->pole_centralregion->add($post);

                    if($this->db->affected_rows() > 0) {
                        $this->session->set_flashdata('success', '<strong>Well done!</strong> You successfully add <a href="#" class="alert-link">data</a>.');
                    }
                    redirect('pole');
                }
            }
        }

        if(isset($_POST['edit'])) {
            if($this->pole_centralregion->check_pole($post['pole_name'], $post['pole_name'])->num_rows() > 0) {
                $this->session->set_flashdata('error', "This $post[pole_name] has been saved with same name, try another name please!");
                redirect('pole');
            } else {
                $this->pole_centralregion->edit($post);
            }
        }
    }

    public function del($id) {

        $this->pole_centralregion->del($id);

        if($this->db->affected_rows() > 0) {
            $this->session->set_flashdata('success', '<strong>Well done!</strong> You successfully remove <a href="#" class="alert-link">data</a>.');
        }
        redirect('pole');
    }
}

controller


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

class pole_centralregion extends CI_Model {

    public function get($id = null) {

        $this->db->from('dm_pole');
        if($id != null) {
            $this->db->where('pole_id', $id);
        }
        $this->db->order_by('pole_name', 'asc');
        $query = $this->db->get();
        return $query;
    }

    public function add($post) {

        $params = [
            'pole_name' => $post['pole_name'],
            'coordinate' => $post['coordinate'],
            'photo' => $post['photo'],
            'description' => $post['description'],
        ];

        $this->db->insert('dm_pole', $params);
    }

    public function edit($post) {

        $params = [
            'pole_name' => $post['pole_name'],
            'coordinate' => $post['coordinate'],
            'photo' => $post['photo'],
            'description' => $post['description'],
        ];

        $this->db->where('pole_id', $post['id']);
        $this->db->update('dm_pole', $params);
    }

    public function check_id($code, $id = null) {
        $this->db->from('dm_pole');
        $this->db->where('pole_name', $code);
        if($id != null) {
            $this->db->where('pole_id !=', $id);
        }
        $query = $this->db->get();
        return $query;
    }

    public function del($id) {

    $this->db->where('pole_id', $id);
    $this->db->delete('dm_pole');
    }

}

model

setelah save, tidak muncul foto dalam folderpada kolom photo selalu NULL

avatar vesaldiy
@vesaldiy

6 Kontribusi 0 Poin

Dipost 2 tahun yang lalu

Tanggapan

mau nampilkan seperti ini di databasenya uploads/web/gambar/ponsel-vaio.jpg

maaf, belum paham maksud mas'nya. jadi disamakan gitu ya nama foldernya?

Belum ada Jawaban. Jadi yang pertama Jawaban

Login untuk ikut Jawaban