【问题标题】:Update a product picture in CodeIgniter在 CodeIgniter 中更新产品图片
【发布时间】:2018-05-11 17:28:01
【问题描述】:

我正在尝试制作更新表单,以便用户能够更改产品图片。表单工作正常,除了图片,所有内容都会更新。

我还有一个 product_foto_thumb 和一个普通的 product_foto,所以我不知道该怎么做。

这就是我的看法:

<input type="file" name="userfile"/>
<td>
    <?php echo form_input(array('type'=>'hidden','id'=>'product_foto', 'name'=>'product_foto', 'value' => $product['product_foto']));?>
</td>

但我想我还需要 product_foto_thumb 的输入类型?

这是我的更新控制器功能:

public function update_product() 
{

    $id= $this->input->post('product_id');
    $data = array(
        'product_naam' => $this->input->post('product_naam'),
        'category_id' => $this->input->post('category_id'),
        'ophaal_plaats' => $this->input->post('ophaal_plaats'),
        'product_beschrijving' => $this->input->post('product_beschrijving'),
        'product_foto_thumb' => 'thumb_'.$dataThumb["raw_name"].$dataThumb['file_ext'],
        'product_foto' => 'new_'.$data["raw_name"].$data['file_ext'],
    );
    $this->Update_product_model->update_product_function($id,$data);
}

所以表单可以正常工作,但是除非我选择了一张新图片并提交了表单,否则旧图片会被删除,并且产品上没有新图片。 谁能帮帮我?

【问题讨论】:

    标签: php codeigniter upload codeigniter-3


    【解决方案1】:

    试试这个代码,肯定会对你有所帮助。

    if($this->input->post('submit')){
        if(($_FILES['userfile'])){
            $errors= array();
            $file_name = $_FILES['userfile']['name'];
            $file_size = $_FILES['userfile']['size'];
            $file_tmp = $_FILES['userfile']['tmp_name'];
            $file_type = $_FILES['userfile']['type'];
            $ext = pathinfo($file_name, PATHINFO_EXTENSION);
            $expensions= array("jpeg","jpg","png");
            // this is used for unique file name always
            $mtime = uniqid(microtime());
            $uniqueid =  substr($mtime,2,8);
            $filename = $uniqueid.'.'.$ext;
    
            if(in_array($ext,$expensions)=== false){
             $errors[]="extension not allowed, please choose a JPEG or PNG file.";
            }
            if($file_size > 2097152) {
             $errors[]='File size must be excately 2 MB';
            }
            if(empty($errors)==true) {
             move_uploaded_file($file_tmp,"upload_folder_name_here/".$filename);
            }
        }
        $id= $this->input->post('product_id');
        $data = array(
            'product_naam' => $this->input->post('product_naam'),
            'category_id' => $this->input->post('category_id'),
            'ophaal_plaats' => $this->input->post('ophaal_plaats'),
            'product_beschrijving' => $this->input->post('product_beschrijving'),
            'product_foto_thumb' => 'thumb_'.$filename,
            'product_foto' => 'new_'.$filename,
        );
        $this->Update_product_model->update_product_function($id,$data);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-06
      • 1970-01-01
      相关资源
      最近更新 更多