【问题标题】:How to upload image file on codeigniter 3?如何在codeigniter 3上上传图像文件?
【发布时间】:2020-12-21 02:29:51
【问题描述】:

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

控制器

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>

【问题讨论】:

    标签: file-upload codeigniter-3


    【解决方案1】:

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

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

    谢谢

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-08
      • 1970-01-01
      • 2023-03-22
      • 2016-01-30
      相关资源
      最近更新 更多