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

时间:2013-05-14 20:35:46

标签: php forms codeigniter

我需要一些帮助来找出编辑页面。

我有插入数据和编辑数据。

在编辑数据上也请求文件/图像上传表单,并在提交时将空值发送到列文件/图像。

我需要的是在编辑时如果文件/图像输入上没有新文件来跳过此输入。

这是我的控制器,用于插入和编辑数据。

// 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');
        }

感谢任何帮助。

解决方案:

// 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 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

如果($ _ FILES ['image'] [tmp_name]!='')将正常工作......

相关问题