如何在Codeigniter中更新图像?

时间:2018-07-09 09:20:51

标签: php mysql codeigniter

我创建的图片上传效果很好,但我也希望对其进行更新。当我添加需要的图像时,它会正确更新,但是如果我不想更改图像并将其保持原样,则无法检索我的当前图像。我只想有时更新我的​​factory_name(field name)。当我更新映像时,它已成功更新,但是当我更新factory_name时,仅现有映像丢失了。请帮我怎么做?

此我的视图文件

            <form action="<?php echo base_url('login/insert_factory/'.$id); ?>" method="POST" enctype="multipart/form-data">
 <?php
           $id =$this->uri->segment(3);
           if($id == "")
            {
            ?>  
            <div class="form-group">
                <label class="form-label" for="field-1">Factory Name</label>
                <!--span class="desc">e.g. "Beautiful Mind"</span-->
                <div class="controls">
                <input type="text" name="factory_name" class="form-control" >
                </div>
            </div>
             <?php

                }
                else
               { ?> 
           <div class="form-group">
                <label class="form-label" for="field-1">Factory Name</label>
                <!--span class="desc">e.g. "Beautiful Mind"</span-->
                <div class="controls">
                    <input type="text" name="factory_name" value="<?php echo $result->factory_name; ?>" class="form-control" >
                </div>
            </div>
            <?php } ?>

           <?php
                            if($id == "")
                            {
                            ?>  

            <div class="form-group">
                <label class="form-label" for="field-1">Upload Logo</label>
                <!--span class="desc">e.g. "Beautiful Mind"</span-->
                <div class="controls">
                     <input type="file"  name="upload_logo" />
                </div>
            </div>
            <?php

                }
                else
               { ?> 
           <div class="form-group">
                <label class="form-label" for="field-1">Upload Logo</label>
                <!--span class="desc">e.g. "Beautiful Mind"</span-->
                <div class="controls">
                     <input type="file"  name="upload_logo" value="<?php echo $result->factory_logo; ?>" />
                </div>
            </div>
           <?php } ?>
           <?php
                            if($id == "")
                            {
                            ?>  
            <div class="form-group">
                <div class="controls">
            <input type="submit" class="btn btn-primary" value="submit" />        

                </div>
            </div>
            <?php

                }
                else
               { ?> 
           <div class="form-group">
                <div class="controls">
            <input type="submit" class="btn btn-primary" value="Update"  />        

                </div>
            </div>
             <?php } ?>
            </form>

这是我的控制器代码:

 public function insert_factory()
     { 
        $id =$this->uri->segment(3);

        if($id == "")
        {
        if(isset($_FILES['upload_logo'])){

              $errors= array();
              $file_name = $_FILES['upload_logo']['name'];
              $file_tmp =$_FILES['upload_logo']['tmp_name'];

              if(empty($errors)==true){
                 move_uploaded_file($file_tmp,"assets/uploads/".$file_name);

                // echo "Success";
              }else{
                 print_r($errors);
              }

                $fields = array(
                   'factory_name' => $this->input->post('factory_name',True),
                   'factory_logo' => $file_name
                );

                $result = $this->login_model->insert_factory($fields);

                if($result){
                    $this->session->set_flashdata('success_msg',' Factory Created Successfully!');
                    redirect('login/view_factory');
                }else{
                    $this->session->set_flashdata('error_msg','Factory Cannot Created.!');
                    redirect('login/create_factory');
                }
            } 
        }else{

            if(isset($_FILES['upload_logo'])){

              $errors= array();
              $file_name = $_FILES['upload_logo']['name'];
              $file_tmp =$_FILES['upload_logo']['tmp_name'];

              if(empty($errors)==true){
                 move_uploaded_file($file_tmp,"assets/uploads/".$file_name);

                // echo "Success";
              }else{
                 print_r($errors);
              }

                $fields = array(
                   'factory_name' => $this->input->post('factory_name',True),
                   'factory_logo' => $file_name
                );

                $this->db->where('factory_id', $id);
                $this->db->update('create_factory', $fields);
                if($this->db->affected_rows()>0)
                {
                    $this->session->set_flashdata('success_msg',' Factory Updated Successfully!');
                }else{
                    $this->session->set_flashdata('error_msg','Factory Cannot Updated.!');
                    redirect('login/create_factory');
                }
                redirect(base_url('login/view_factory'));
        } 

        }
     }

这是我的模型文件:

    public function insert_factory($fields)
        {
            $this->db->insert('create_factory',$fields);
            return ($this->db->affected_rows() != 1) ? false : true;
        }

1 个答案:

答案 0 :(得分:1)

在控制器的if和else块中,请修改您的现有代码,以在未提供图像且仅提供出厂名称时不更新空白图像。

if和else块中的这段代码:

$fields = array(
    'factory_name' => $this->input->post('factory_name',True),
    'factory_logo' => $file_name
);

需要更改为:

$fields['factory_name'] = $this->input->post('factory_name', True);

if (!empty($file_name)) {
    $fields['factory_logo'] = $file_name;
}