如何在codeigniter 3上传图像文件



我已经尝试了所有的代码上传图像文件,但我仍然卡住了,它一直存在错误。它无法检测图像文件的数据,所以当我提交表单时,它无法存储到数据库中。我看到了我搜索过的每一个教程,并查看了我的代码,似乎一切都很好,但为什么它仍然存在错误。

控制器

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');
}
}
}

型号

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);
}

查看

<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>

已解决。我只需要将此代码添加到博客控制器

if (empty($_FILES['blogHeaderImg']['name'])) {
$this->form_validation->set_rules('blogHeaderImg', 'Document', 'required');
}

而不是使用这个代码

$this->form_validation->set_rules('blogHeaderImg', 'Header Image tidak boleh kosong', 'required');

谢谢

相关内容

  • 没有找到相关文章

最新更新