【问题标题】:Image update in codeignitercodeigniter 中的图像更新
【发布时间】:2018-03-08 17:16:49
【问题描述】:

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

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

提前致谢

这是我的代码:

控制器

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

视图(编辑视图)

<?php echo form_open('posts/update'); ?>
    <input type="hidden" name="id" value="<?php echo $post['id']; ?>">
    <input type="hidden"  id="old"  name="old"  value="<?php echo $post['post_image']   ?>">
    
    <div class="form-group">
        <label>Title</label>
        <input type="text" class="form-control" name="title" placeholder="Add Title" value="<?php echo $post['title']; ?>">
    </div>
    
    <div class="form-group">
        <label>Body</label>
        <textarea id="editor1" class="form-control" name="body" placeholder="Add Body"><?php echo $post['body']; ?></textarea>
    </div>

    <div class="form-group">
        <label>Change Image</label>
        <input class="form-control" type="file" name="userfile" size="20">
    </div>      
    <button type="submit" class="btn btn-default">Submit</button>
</form>

【问题讨论】:

  • 当您尝试更新帖子时,您是再次上传图片还是保持原样?或者在更新表单value="&lt;?php echo $post['post_image'] 这是图像名称?
  • 这样做exit($this-&gt;upload-&gt;display_errors());,而不是你目前的上传错误,看看它是否说明了什么。
  • 嗨,Alex,感谢您的回复。 “post_image”是我数据库中的图像名称字段。我试过 exit($this->upload->display_errors());但没有错误信息。当我尝试上传图片时,通常如果选择了一张图片进行上传,它应该使用它,否则它会保留旧的

标签: php codeigniter


【解决方案1】:

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

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

编辑:

我的错,显然do_upload() 上传的默认文件名参数是userfile。我现在猜测为什么它不起作用是您的“更新”表单不使用form_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;
}

用法:

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

【讨论】:

  • 更新工作正常,但图像没有更新,所以即使我使用 do_upload('userfile') 也没有错误。我收到消息('您的帖子已成功更新!')
  • 我已经尝试过一些调试,但问题是在更新函数中,'userfile' 是未定义的。我刚刚将 "if( $_FILES['userfile']['name'] != '' ) 更改为 if( $_FILES['userfile']['name'] == '' )。但我试图弄清楚如何解决它。
  • 我认为问题出在upload_image() 函数中。我使用相同的函数进行创建和更新,它适用于创建,但不适用于更新(它无法读取 $_FILES['userfile']['name'] 来更新)。不知道为什么?
  • 好吧,我不知道为什么您会期望看不到“您的帖子已成功更新!”因为如果上传功能产生错误,你什么都不做。 if(!$this-&gt;upload-&gt;do_upload()) { exit($this-&gt;upload-&gt;display_errors()); } 试试这个看看发生了什么
  • 并且在查看您的视图后,您需要将form_open 更改为form_open_multipart
猜你喜欢
  • 1970-01-01
  • 2015-06-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-07-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多