【问题标题】:Codeigniter making image upload optional in a formCodeigniter 在表单中使图像上传成为可选
【发布时间】:2013-02-07 23:39:35
【问题描述】:

我有一个包含三个字段的表单——一个输入字段、一个文本区域和一个图像上传。 当我填写所有内容并选择要上传的图像时,该表单可以正常上传图像,但是当我不选择图像时,它会显示选择和图像。即使用户不上传图片,我也希望提交表单。我确信我昨天有一个解决方案,但它今天停止工作。 控制器:

        function store()
{
$this->output->enable_profiler(TRUE);

        $this->load->model('campus_m');
                    $config['upload_path'] = './uploads/';
                    $config['allowed_types'] = 'gif|jpg|png|jpeg';
                    $config['max_size'] = '100';
                    $config['max_width']  = '1024';
                    $config['max_height']  = '768';
                    $config['file_name'] = preg_replace('/[^a-z0-9]+/i','-',iconv('UTF-8','ASCII//TRANSLIT',$this->input->post('name')));
                    $config['file_name'] = trim($config['file_name'],'-').now().'.jpg';                         

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

                            $this->load->helper(array('form', 'url'));

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

                        $this->form_validation->set_rules('goods', 'Goods', 'required');
                        $this->form_validation->set_rules('name', 'Name', 'required|max_length[12]');
                        if ($this->form_validation->run() == FALSE)
                        {
                                $this->load->view('campus_write_v');
                        }
                        else
                        {
                                    if (empty($_FILES['userfile'])) {
                    print_r($_FILES['userfile']);
                                if(!$query = $this->campus_m->create_review("Marla-overdoses1360186300.jpg")){
                                    $data['write_campus'] = 'The answer has not been stored.';
                                    $this->load->view('campus_write_v', $data);
                                    }
                                else {
                                    $data['write_campus'] = 'The answer has been stored. ';
                                    $this->load->view('campus_write_v', $data);
                                }   
                                    }
                                    else{
                                         if($this->upload->do_upload()){
                                if(!$query = $this->campus_m->create_review($config['file_name'])){
                                    $data['write_campus'] = 'The answer has not been stored.';
                                    $this->load->view('campus_write_v', $data);
                                    }
                                else {
                                    $data['write_campus'] = 'The answer has been stored. ';
                                    $this->load->view('campus_write_v', $data);
                                }                                               
                                    }
                            else
                            {
                                 $error = array('error' => $this->upload->display_errors());
                                 foreach ($error as $rows => $r) {
                                 echo $r ;                                 
                                 }
                                    $this->load->view('campus_write_v');                             
                            }
                            }
                        }
                    }

【问题讨论】:

    标签: php codeigniter


    【解决方案1】:
        if(isset($_FILES))
        {
    
              if(empty($_FILES['file']['name']))
              {
                //logic here    
              }else{
                    if(//validation here )
                    {
                       //logic here
                    }
    
              }
    
        }
    

    【讨论】:

    • 我之前试过,但是上传图片出错,不上传图片,只是将默认文件的名称保存到数据库中。它执行“else”语句,但每次都执行包含“empty”子句的“if”块。数组([名称] => 图像 (1).jpg [类型] => 图像/jpeg [tmp_name] => C:\xampp\tmp\php3EC6.tmp [错误] => 0 [大小] => 9852)
    • 答案是 $_FILES 不为空,如果你 var_dump($_FILES) 里面没有任何东西,它会返回一个数组,其中包含一些值,使得 empty($_FILES) 返回 false,你必须先测试如果是isset那么你就会知道一个文件上传已经提交了,那么你会检测文件是否有名字,看你刚才测试整个文件的代码,
    • 我确实尝试过 if (empty($_FILES['userfile']['userfile'])) 但它仍然使用包含的名称“userfile”也是文件的名称。
    • php 中没有名为 userfile 的文件的属性,只有 name,size,type,tmp_name,errorphp.net/manual/en/reserved.variables.files.php看手册你得用$_FILES['userfile']['name']
    • @tomexans 文件上传的输入是这样的:
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-12-13
    • 2018-07-31
    • 1970-01-01
    • 1970-01-01
    • 2018-12-26
    • 1970-01-01
    • 2011-11-03
    相关资源
    最近更新 更多