I've tried everything to code upload the image file but I still stuck and it keeps an error. It can't detect the data of the image file so it can't store to the database when I submitted the form. I saw every tutorial I've been searched and look into my code seems everything right but why it still keeps an error.
Controller
public function create() { if (!$this->session->userdata('user_logged')) { redirect('Auth'); } $data["title"] = "Form Create Blog"; $data["landingpage"] = false; $data['content'] = 'component/admin/blog/blog_create'; $this->form_validation->set_rules('blogTitle', 'Title tidak boleh kosong', 'required|max_length[50]'); $this->form_validation->set_rules('blogHeaderImg', 'Header Image tidak boleh kosong', 'required'); $this->form_validation->set_rules('blogKeyword', 'Keyword tidak boleh kosong', 'required|max_length[50]'); $this->form_validation->set_rules('blogContent', 'Content tidak boleh kosong', 'required'); if ($this->form_validation->run() == FALSE) { $this->load->view('index', $data); } else { $config['upload_path'] = realpath(APPPATH . '../assets/img/upload/blog/header_image'); $config['allowed_types'] = 'jpg|png|PNG'; $nmfile = time() . "_" . $_FILES['blogHeaderImg']['name']; $config['file_name'] = $nmfile; $this->load->library('upload', $config); if (!$this->upload->do_upload("blogHeaderImg")) { $error = array('error' => $this->upload->display_errors()); echo '<div class="alert alert-danger">' . $error['error'] . '</div>'; } else { $data = array('upload_data' => $this->upload->data()); $header_image = $data['upload_data']['file_name']; $this->M_Blog->storeBlogData($header_image); print_r($_FILES['blogHeaderImg']); $this->session->set_flashdata('flashAddBlog', 'Data berhasil <strong>ditambahkan</strong>'); redirect('blog'); } } }
Model
public function storeBlogData($header_image) { $data = [ 'title' => $this->input->post('blogTitle', TRUE), 'header_image' => $header_image, 'content' => $this->input->post('blogContent', TRUE), 'blog_keyword' => $this->input->post('blogKeyword', TRUE), 'created_by' => $this->session->userdata('user_logged')->id, 'last_modified_by' => $this->session->userdata('user_logged')->id, 'is_deleted' => 'n' ]; $this->db->insert('blog', $data); }
View
<form method="POST" action="create" enctype="multipart/form-data"> <div class="form-group"> <label for="blogTitle">Title</label> <input class="form-control" type="text" name="blogTitle" id="blogTitle" placeholder="Title"> <small class="form-text text-danger"><?= form_error('blogTitle') ?></small> </div> <div class="form-group"> <label for="blogHeaderImg">Header Image</label> <input class="form-control-file" type="file" id="blogHeaderImg" name="blogHeaderImg"> <small class="form-text text-danger"><?= form_error('blogHeaderImg') ?></small> </div> <div class="form-group"> <label for="blogKeyword">Keyword</label> <input class="form-control" type="text" id="blogKeyword" name="blogKeyword" placeholder="Keyword"> <small class="form-text text-danger"><?= form_error('blogKeyword') ?></small> </div> <div class="form-group"> <label for="blogContent">Content</label> <textarea class="form-control" type="text" id="blogContent" name="blogContent" placeholder="Content" rows="10"></textarea> <small class="form-text text-danger"><?= form_error('blogContent') ?></small> </div> <button class="btn btn-primary" type="submit">Submit</button> </form>
https://stackoverflow.com/questions/65386864/how-to-upload-image-file-on-codeigniter-3 December 21, 2020 at 10:29AM
没有评论:
发表评论