codeigniter中的图像更新

时间:2018-03-08 17:16:50

标签: php codeigniter

我在CodeIgniter中为帖子更新图片时遇到问题。

我创建带图片的帖子没有问题,但是当我尝试更新帖子时,发布更改的信息会更新而不是图像。 我从不同的教程中尝试了几个没有成功的可能性

提前致谢

这是我的代码:

控制器

#!/bin/bash

if [ -t 0 ]; then stty -echo -icanon -icrnl time 0 min 0; fi

keypress=''
while [ "$keypress" != "s" ]; do
  echo text text text
  keypress="`cat -v`"
done

if [ -t 0 ]; then stty sane; fi

模型

public function create(){   

    if(!$this->session->userdata('logged_in'))
    {
        redirect('users/login');
    }
    $data['title'] = 'Create Post';

    $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 
    {
        $slug = url_title($this->input->post('title'));

        $post_image =  $this->upload_image();           

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

        //$this->post_model->create_post($post_image);
        $this->post_model->create_post($data);

        // Set message
        $this->session->set_flashdata('post_created', 'Your post has been created successfully!');
        redirect('posts');
    }
}

public function edit($slug)
{       
    $data['post'] = $this->post_model->get_posts($slug);

    // Check if logged user has created this post 
    if($this->session->userdata('user_id') != $this->post_model->get_posts($slug)['user_id'])
    {
        redirect('posts');
    }
    $data['categories'] = $this->category_model->get_categories();

    if(empty($data['post']))
    {
        show_404();
    }

    $data['title'] = 'Edit Post';

    $this->load->view('templates/header');
    $this->load->view('posts/edit', $data);
    $this->load->view('templates/footer');
}

public function update()
{
    // Check login
    if(!$this->session->userdata('logged_in')){
        redirect('users/login');
    }
    $id=$this->input->post("id");

    $slug = url_title($this->input->post('title'));

    if( $_FILES['userfile']['name']!="" )
    {
        $post_image = $this->upload_image();
    }
    else
    {
        $post_image=$this->input->post('old');
    }

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

    //$this->post_model->update_post($post_image);
    $this->post_model->update_post($data,$id);

    // Set message
    $this->session->set_flashdata('post_updated', 'Your post has been updated successfully!');
    redirect('posts');
}

public function upload_image()
{       
    // Upload Image
    $config['upload_path'] = './assets/images/posts';
    $config['allowed_types'] = 'gif|jpg|jpeg|png';
    $config['max_size'] = '2048';
    $config['max_width'] = '2000';
    $config['max_height'] = '2000';

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

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

查看(编辑视图)

public function create_post($data)
{
   return $this->db->insert('posts', $data);
}   
public function update_post($data, $id)
{
    $this->db->where('id', $id);            
    return $this->db->update('posts', $data);
}

1 个答案:

答案 0 :(得分:0)

由于do_upload()没有声明文件字段的名称,我不确定上传的内容。在这种情况下,我认为它是userfile,例如do_upload('userfile')

考虑使用文件上传错误变量做一些事情,因为这应该很快就发现了你的错误。

编辑:

我上传的do_upload()默认文件名参数userfileform_open_multipart。我现在猜测它为什么不起作用是你的更新"表格,不使用private function upload_image() { // Upload Image $config['upload_path'] = './assets/images/posts'; $config['allowed_types'] = 'gif|jpg|jpeg|png'; $config['max_size'] = '2048'; $config['max_width'] = '2000'; $config['max_height'] = '2000'; $this->load->library('upload', $config); if(!$this->upload->do_upload()) { throw new Exception($this->upload->display_errors()); //$errors = array('error' => $this->upload->display_errors()); //$post_image = 'noimage.jpg'; } else { $upload_data=$this->upload->data(); $post_image=$upload_data['file_name']; } return $post_image; }

请注意:您还可以将重定向功能移回登录控制器构造函数。这将消除一些重复代码行。

此外,如果您正在寻找 easy 方式来获取上传功能的错误,您可以执行以下操作:

try {
    $this->upload_image();
} catch (Exception $e) {
    show_error($e->getMessage());
}

用法:

for (var i = 0; i = exercise.exercise.length; i++){