drjun1994 2013-05-14 20:35
浏览 37

Codeigniter:如何在没有编辑图像输入的情况下编辑表单

I need a small help to figure out the edit page.

I have the Insert data and Edit data.

On edit data is requesting also the form for file/image upload, and on submit this send the null to the column file/image.

What i need is that on edit if no new file is on file/image input to skip this input.

This is my controller to insert and edit data.

// Set up the form
        $rules = $this->image_m->rules;
        $this->form_validation->set_rules($rules);

        // Upload File Image
        $status = "";
        $msg = "";
        $file_element_name = 'file_name';

        if ($status != "error")
        {
          $config['upload_path'] = './uploads/images/';
          $config['allowed_types'] = 'gif|jpg|png|doc|txt';
          $config['max_size']   = '2000';
          $config['max_width']  = '1024';
          $config['max_height']  = '768';
          $config['encrypt_name'] = FALSE;
          $config['remove_spaces'] = TRUE;

          $this->load->library('upload', $config);

          if (!$this->upload->do_upload($file_element_name))
          {
             $status = 'error';
             $msg = $this->upload->display_errors('', '');
          }
          else
            {
             $image = $this->upload->data();
             }
          }



        // Process the form
        if ($this->form_validation->run() == TRUE) {
            $data = array(
                'pubdate' => $this->input->post('pubdate'),
                'enddate' => $this->input->post('enddate'),
                'name' => $this->input->post('name'),
                'image'     => $image['file_name']
            );
            $this->image_m->save($data, $id);
            redirect('admin/image');
        }

Any help is appreciated.

Solution:

// Process the form
        if ($this->form_validation->run() == TRUE) {
            if(is_null($image['file_name'])){
            $data = array(
                'pubdate' => $this->input->post('pubdate'),
                'enddate' => $this->input->post('enddate'),
                'name' => $this->input->post('name'),
                'caption' => $this->input->post('caption')
            );
            } else {
            $data = array(
                'pubdate' => $this->input->post('pubdate'),
                'enddate' => $this->input->post('enddate'),
                'name' => $this->input->post('name'),
                'image'     => $image['file_name'],
                'caption' => $this->input->post('caption')
            );
            }
  • 写回答

2条回答 默认 最新

  • douji2283 2013-05-14 21:36
    关注
    $img = null;
    
    //Check if a file is being uploaded/updated
    if($_FILES['fieldname'][tmp_name] != ''){
       //upload file here.....
    
       $file = $this->upload->data();
       $img  = $file['file_name']; 
    }
    
    //if no file name assigned to var, use the data stored in the model
    //default_image.jpg
    if(is_null($img)) $img = (string)$model->image;
    

    -

    `image` varchar(255) not null default 'default_image.jpg'
    
    评论

报告相同问题?

悬赏问题

  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示