gpt4 book ai didi

php - 如何更新codeigniter中的图像?

转载 作者:行者123 更新时间:2023-11-29 10:09:35 24 4
gpt4 key购买 nike

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

这是我的 View 文件

            <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>

这是我的 Controller 代码:

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

最佳答案

在您的 Controller 中,在 if 和 else block 中,请修改现有代码,以便在未提供图像且仅提供工厂名称时不更新空图像。

if 和 else block 中的代码:

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

关于php - 如何更新codeigniter中的图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51242077/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com