dongyin8991 2013-05-11 06:16
浏览 41

Codeigniter两个文件上传,一个强制和一个可选

I am trying to upload two files with the help of codeigniter functions. One of the file should be mandatory and one should be optional.

I am using the code below to upload those files but I cannot figure out the way to make one optional and one mandatory. I tried few modifications to the code below, but i bumped into many errors. I am new to codeigniter.

Even the code below for handling the uploads may not be appropriate but it is working.

$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');
                }
            }
  • 写回答

1条回答 默认 最新

  • douqiao4450 2013-05-11 08:28
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程