簡體   English   中英

當我嘗試上傳圖片然后在 Codeigniter 中查看時,我看到的是 noimage.jpg 而不是上傳的圖片

[英]When I try to upload an image then view it in Codeigniter, I see noimage.jpg instead the uploaded one

我正在嘗試使用圖像創建帖子,但是當我嘗試加載它時,它只顯示默認圖像(noimage.jpg)而不是上傳的圖像。 這是我創建帖子的 controller

public function create(){
        $data['title'] = 'Crear Entrada';

        $data['categories'] = $this->post_model->get_categories();

        $this->form_validation->set_rules('title', 'Title', 'required');
        $this->form_validation->set_rules('body', 'Body', 'required');

        if($this->form_validation->run() === FALSE){
            $this->load->view('templates/header');
            $this->load->view('posts/create', $data);
            $this->load->view('templates/footer');
        } else {
            // Upload Image
            $config['upload_path'] = './assets/images/posts';
            $config['allowed_types'] = 'gif|jpg|png';
            $config['max_size'] = '2048';
            $config['max_width'] = '500';
            $config['max_height'] = '500';

            $this->load->library('upload', $config);

            if(!$this->upload->do_upload()){
                $errors = array('error' => $this->upload->display_errors());
                $post_image = 'noimage.jpg';
            } else {
                $data = array('upload_data' => $this->upload->data());
                $post_image = $_FILES['userfile']['name'];
            }

            $this->post_model->create_post($post_image);
            redirect('posts');
        }

    }

model:

public function create_post($post_image){
        $slug = url_title($this->input->post('title'));

        $data = array(
            'title' => $this->input->post('title'),
            'slug' => $slug,
            'body' => $this->input->post('body'),
            'category_id' => $this->input->post('category_id'),
            'post_image' => $post_image
        );

        return $this->db->insert('posts', $data);

這是 HTML 我嘗試從中加載圖像

    <div class="form-group">
    <label>Cargar Imagen</label>
    <input type="file" name="userfile" size="20">
    </div>

第 1 步:提供要發布的文件數組的名稱,將$this->upload->do_upload()替換$this->upload->do_upload('userfile')並通過添加print_r($this->upload->display_errors()); 了解您在上傳文件時面臨的確切錯誤。

第 2 步:也始終使用$this->upload->data()提供的文件名。 在您的情況下,它看起來像這樣$data['upload_data']['file_name']

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM