0

我正在尝试借助 codeigniter 函数上传两个文件。其中一个文件应该是强制性的,一个应该是可选的。

我正在使用下面的代码上传这些文件,但我无法弄清楚如何使一个可选和一个强制。我尝试对下面的代码进行一些修改,但遇到了很多错误。我是codeigniter的新手。

即使下面用于处理上传的代码也可能不合适,但它正在工作。

$config['upload_path'] = 'uploads/';
        $path=$config['upload_path'];
        $config['allowed_types'] = '*';

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

            $i=0;
            foreach ($_FILES as $key => $value)
            {
                if (!empty($key['name']))
                {

                    $this->upload->initialize($config);
                    if (!$this->upload->do_upload($key))
                    {
                            $errors = $this->upload->display_errors();
                            $this->session->set_flashdata('error', $errors);
                            redirect(base_url().'upload', 'refresh');
                    }
                    else
                    {
                        $data = array('upload_data' => $this->upload->data());
                        $p[$i] = $this->upload->data();
                    }
                }
                $i++;
            } //endforeach
            if(empty($errors)){
                //if there are no errors, write it into the database
                $data = array('user_id'=>$this->session->userdata('id'),
                              'name'=>$this->input->post('name'),
                              'screenshot'=>$p[1]['file_name'],
                              'model'=> $p[0]['file_name'],
                              'created'=>date('Y-m-d H:i:s')
                );
                if($this->usermodel_model->save($data)){
                    //success
                    redirect(base_url().'dashboard?success');
                }else{
                    //failed
                    redirect(base_url().'upload');
                }
            }
4

1 回答 1

0

Sohanmax put $i++; inside if (!empty($key['name'])){ } and after ending foreach check if($i !=0) if it's false show the error, hope it'll work.

于 2013-05-11T08:28:37.690 回答