Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/232.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何在codeigniter中更新产品图片_Php_Codeigniter_Codeigniter 3 - Fatal编程技术网

Php 如何在codeigniter中更新产品图片

Php 如何在codeigniter中更新产品图片,php,codeigniter,codeigniter-3,Php,Codeigniter,Codeigniter 3,我试图更改控制器中的更新功能, 还添加了删除模型中的旧图片功能 My update function in controller: public function update_product() { $id = $this->input->post('product_id'); $this->load->library('image_lib'); // remove old images $this->Prod

我试图更改控制器中的更新功能, 还添加了删除模型中的旧图片功能

My update function in controller:

        public function update_product() {

    $id = $this->input->post('product_id');
    $this->load->library('image_lib');

    // remove old images
    $this->Product_model->remove_old_images($id);


        if(!$this->update->do_update('userfile')){
            $error = array('error'=>$this->update->display_errors());
            $this->load->view('product_form', $error);
        }else{
           //Main image
            $data = $this->upload->data();
            $config['image_library'] = 'gd2';
            $config['source_image'] = './upload/'.$data["raw_name"].$data['file_ext'];
            $config['new_image'] = './upload/'.'new_'.$data["raw_name"].$data['file_ext'];
            $config['create_thumb'] = FALSE;
            $config['maintain_ratio'] = FALSE;
            $config['width'] = 547;
            $config['height'] = 430;

}

        // make thumb
        $configThumb['image_library'] = 'gd2';
        $configThumb['source_image'] = './upload/'.$dataThumb["raw_name"].$dataThumb['file_ext'];
        //$config2['create_thumb'] = TRUE;
        $configThumb['new_image'] = './upload/'.'thumb_'.$dataThumb["raw_name"].$dataThumb['file_ext'];
        $configThumb['maintain_ratio'] = TRUE;
        $configThumb['width'] = 120;
        $configThumb['height'] = 112;
        $this->load->library('image_lib', $configThumb);
        if (!$this->image_lib->resize()) {
            $this->session->set_flashdata('error', $this->image_lib->display_errors());
            redirect('/Product/cadeaupagina_aangeboden');
        }

         $this->image_lib->initialize($configThumb);
         $this->image_lib->resize();


     $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' => 'new_'.$data["raw_name"].$data['file_ext'],
    'product_foto_thumb' => 'thumb_'.$dataThumb["raw_name"].$dataThumb['file_ext'],
     );

      $data['img'] = base_url().
      '/upload/new_'.$data["raw_name"].$data['file_ext'];
      $dataThumb['img'] = base_url().
     '/upload/thumb_'.$dataThumb["raw_name"].$dataThumb['file_ext'];

     $this->Product_model->update($id, $data, $dataThumb);

     $this->load->view('cadeaubewerken',$data); 
     redirect('/Product/cadeaupagina_aangeboden');


}

My model update and remove image functions:



    public function get($id = '') {
            if (!empty($id)) {
                $this->db->where('product_id', $id);
            }
            return $this->db->get_where('products');
        }


                   public function update($id, $data, $dataThumb) {
        return $this->db->update('products', $data, array('product_id' => $id));
    }


         public function remove_old_images($id = null) {
            $i = 0; // num of delete files
            if ($id !== null && $this->get($id)->num_rows() == 1) {
                $fotos = $this->get($id)->row();
                $main = $fotos->product_foto;
                $thumb = $fotos->product_foto_thumb;
                if (is_file($this->up_path . $main)) {
                    unlink($this->up_path . $main);
                    $i++;
                }
                if (is_file($this->up_path . $thumb)) {
                    unlink($this->up_path . $thumb);
                    $i++;
                }
                return $i;
            } else {
                return $i;
            }

         }
我的视图文件格式:

       <tr>
             <h4>Cadeau naam</h4>
          <td><?php echo form_input(array('id'=>'product_naam', 'name'=>'product_naam', 'value' => $product["product_naam"] , 'size'=>25));?></td>
           <td><?php echo form_input(array('type'=>'hidden','id'=>'product_id', 'name'=>'product_id', 'value' => $product['product_id']));?>
        </tr>


         <tr>
          <td><?php echo form_open_multipart('Product/upload'); ?> </td>
        </tr>

         <tr><td>
             <h4>Kies een categorie</h4>
         <select name="category_id">
        <?php foreach (get_categories_h() as $category) :
        if ($category->id ==  $product["category_id"]){
            echo '<option value="'.$category->id .'" selected>' . $category->name . '</option>';
        }else{
            echo '<option value="'.$category->id .'">'.$category->name . '</option>';
            }
        endforeach; ?>
</select>
        </td>
        </tr> 

         <tr>
          <td><h4>Ophaal plaats</h4><?php echo form_input(array('id'=>'ophaal_plaats', 'name'=>'ophaal_plaats', 'value' => $product["ophaal_plaats"], 'size'=>25));?></td>
          <td><?php echo form_input(array('type'=>'hidden','id'=>'ophaal_plaats', 'name'=>'ophaal_plaats', 'value' => $product['ophaal_plaats']));?>
        </tr>

        <tr>
            <td>
       <h4>Gebruik adres van mijn account</h4>
        </td>
        </tr>

        <tr>
            <td>
                <h4>Huidig cadeau profiel foto</h4>
                 <img src="<?php echo base_url(); ?>upload/<?php echo $product['product_foto_thumb']; ?>" class="img-responsive">
            </td>
        </tr>

        <tr>

             <div class="image-upload">
                  <label for="file-input">
            <td><?php echo form_input(array('type'=>'hidden','id'=>'file', 'name'=>'product_foto', 'value' => $product['product_foto']));?>
             <h4>Cadeau profiel foto veranderen</h4>
            <input type="file" id="file1" name="userfile"/>
            </label>



            </div>



    <tr>
    <td><?php echo form_textarea(array('type'=>'textarea','id'=>'product_beschrijving', 'name'=>'product_beschrijving', 'value' =>$product['product_beschrijving'], 'size'=>25));?></td>
    <td><?php echo form_input(array('type'=>'hidden','id'=>'product_beschrijving', 'name'=>'product_beschrijving', 'value' => $product['product_beschrijving']));?>
    </tr>


<tr>
<td><input type="submit" class="btn btn-primary" name="submit" value="Cadeau bewerken!" /></td>
</tr>
     </table>
    </form>
致命错误:在第42行调用堆栈:0.0004 244440 1的/home/ubuntu/workspace/application/controllers/KdGwController.php中对非对象调用成员函数do_update()。{main}()/home/ubuntu/workspace/index.php:0.0005 247232。需要一次('/home/ubuntu/workspace/system/core/CodeIgniter.php')/home/ubuntu/workspace/index.php:3150.0051 928288 3。调用用户函数数组:{/home/ubuntu/workspace/system/core/CodeIgniter.php:532}()/home/ubuntu/workspace/system/core/CodeIgniter.php:532 0.0051 929096 4。KdGwcontroller->update_product()/home/ubuntu/workspace/system/core/CodeIgniter.php:532 遇到一个PHP错误

Severity: Error

Message: Call to a member function do_update() on a non-object

Filename: controllers/KdGwController.php

Line Number: 42

Backtrace:

基本上,您执行的操作与最初上载时相同,但您使用更新查询并在从数据库获取旧文件后删除它们:

public function update_product() {
    $id = $this->input->post('product_id');
    // check if user with current id matches user who created the post
    // we dont want *smart* users to edit others posts
    if ($this->Product_model->get($id)->row()->user_id !== $_SESSION['user_id']) {
        show_error('Not authorized to edit this product.'); // this exits
    }
    if (is_uploaded_file($_FILES['userfile']['tmp_name'])) {
        $this->load->library('upload');
        $this->load->library('image_lib');
        // remove old images
        $this->Product_model->remove_old_images($id);
        // upload new image
        $config['upload_path'] = './upload/';
        $config['allowed_types'] = 'gif|jpg|png|bmp|jpeg';
        $config['encrypt_name'] = true;
        $this->upload->initialize($config);
        if (!$this->upload->do_upload('userfile')) {
            // handle errors here somehow
            //$error = array('error' => $this->upload->display_errors());
            //$this->load->view('product_form', $error);
            echo $this->upload->display_errors(); // just for debug now
        } else {
            $upData = $this->upload->data();
            // set names
            $mainImage = 'new_' . $upData["raw_name"] . $upData['file_ext'];
            $thumbImage = 'thumb_' . $upData["raw_name"] . $upData['file_ext'];
            // Main image
            $configMain['image_library'] = 'gd2';
            $configMain['source_image'] = './upload/' . $upData["raw_name"] . $upData['file_ext'];
            $configMain['new_image'] = './upload/' . $mainImage;
            $configMain['create_thumb'] = FALSE;
            $configMain['maintain_ratio'] = FALSE;
            $configMain['width'] = 547;
            $configMain['height'] = 430;
            // Thumnail image
            $configThumb['image_library'] = 'gd2';
            $configThumb['source_image'] = './upload/' . $upData["raw_name"] . $upData['file_ext'];
            $configThumb['new_image'] = './upload/' . $thumbImage;
            $configThumb['create_thumb'] = TRUE;
            $configThumb['maintain_ratio'] = FALSE;
            $configThumb['width'] = 120;
            $configThumb['height'] = 112;
            $this->image_lib->initialize($configMain);
            $this->image_lib->resize();
            $this->image_lib->initialize($configThumb);
            $this->image_lib->resize();
            // delete original
            @unlink($upData['full_path']);
            // we add filesnames to db $data array
            $data['product_foto'] = $mainImage;
            $data['product_foto_thumb'] = $thumbImage;
        }
    }
    $data = array(
        'product_naam' => $this->input->post('product_naam'),
        'category_id' => !empty($this->input->post('category_id')) ? $this->input->post('category_id') : 0,
        'ophaal_plaats' => $this->input->post('ophaal_plaats'),
        'product_beschrijving' => $this->input->post('product_beschrijving'),
        'date_updated' => date('Y-m-d')
    );
    $this->Product_model->update($id, $data);
    echo 'Done'; // handle success however you want... just for debug now
}

请解释“'product_foto'=>'new_.$data[“raw_name”]。$data['file_ext']”这是一个错误,我编辑了我的问题。我认为:'value'=>$product['product_foto']);?>但是我是否也必须提到图片存储的文件夹名称?这个问题是否解决了?。。。如果未解决,请提供模型功能。确定尚未解决,我将编辑我的问题,以便向您展示我的模型功能。注释不用于扩展讨论;这段对话已经结束。好的,谢谢。我会尝试一下这段代码,看看它是否有效,而且模型保持不变,对吗?像这样的更新函数:公共函数update($id,$data){return$this->db->update('products',$data,array('product_id'=>$id));}和删除旧图像函数保持不变,对吗?另外我还有一个问题,当用户输入表单并且只更改文本而不是图片等其他字段时,折叠图片被删除,但我只想在用户选择新图片时删除旧图片,我需要使用Javascript吗?因此,当用户单击“选择新图片”按钮时,旧图片将被删除,我不明白。在我的代码中,只有在添加并提交了文件的情况下才会删除旧图片。这真的很奇怪,我使用的是相同的代码,即使我没有选择新图片,在提交表单时旧图片也会被删除。你认为会有什么问题?
public function update_product() {
    $id = $this->input->post('product_id');
    // check if user with current id matches user who created the post
    // we dont want *smart* users to edit others posts
    if ($this->Product_model->get($id)->row()->user_id !== $_SESSION['user_id']) {
        show_error('Not authorized to edit this product.'); // this exits
    }
    if (is_uploaded_file($_FILES['userfile']['tmp_name'])) {
        $this->load->library('upload');
        $this->load->library('image_lib');
        // remove old images
        $this->Product_model->remove_old_images($id);
        // upload new image
        $config['upload_path'] = './upload/';
        $config['allowed_types'] = 'gif|jpg|png|bmp|jpeg';
        $config['encrypt_name'] = true;
        $this->upload->initialize($config);
        if (!$this->upload->do_upload('userfile')) {
            // handle errors here somehow
            //$error = array('error' => $this->upload->display_errors());
            //$this->load->view('product_form', $error);
            echo $this->upload->display_errors(); // just for debug now
        } else {
            $upData = $this->upload->data();
            // set names
            $mainImage = 'new_' . $upData["raw_name"] . $upData['file_ext'];
            $thumbImage = 'thumb_' . $upData["raw_name"] . $upData['file_ext'];
            // Main image
            $configMain['image_library'] = 'gd2';
            $configMain['source_image'] = './upload/' . $upData["raw_name"] . $upData['file_ext'];
            $configMain['new_image'] = './upload/' . $mainImage;
            $configMain['create_thumb'] = FALSE;
            $configMain['maintain_ratio'] = FALSE;
            $configMain['width'] = 547;
            $configMain['height'] = 430;
            // Thumnail image
            $configThumb['image_library'] = 'gd2';
            $configThumb['source_image'] = './upload/' . $upData["raw_name"] . $upData['file_ext'];
            $configThumb['new_image'] = './upload/' . $thumbImage;
            $configThumb['create_thumb'] = TRUE;
            $configThumb['maintain_ratio'] = FALSE;
            $configThumb['width'] = 120;
            $configThumb['height'] = 112;
            $this->image_lib->initialize($configMain);
            $this->image_lib->resize();
            $this->image_lib->initialize($configThumb);
            $this->image_lib->resize();
            // delete original
            @unlink($upData['full_path']);
            // we add filesnames to db $data array
            $data['product_foto'] = $mainImage;
            $data['product_foto_thumb'] = $thumbImage;
        }
    }
    $data = array(
        'product_naam' => $this->input->post('product_naam'),
        'category_id' => !empty($this->input->post('category_id')) ? $this->input->post('category_id') : 0,
        'ophaal_plaats' => $this->input->post('ophaal_plaats'),
        'product_beschrijving' => $this->input->post('product_beschrijving'),
        'date_updated' => date('Y-m-d')
    );
    $this->Product_model->update($id, $data);
    echo 'Done'; // handle success however you want... just for debug now
}