0

我需要一点帮助来弄清楚编辑页面。

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

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

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

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

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

2 回答 2

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'
于 2013-05-14T21:36:26.633 回答
0

if($_FILES['image'][tmp_name] != '') 可以正常工作...

于 2013-05-17T09:57:53.427 回答