Codeigniter 2.x可选文件上传问题“您没有选择要上传的文件”

时间:2016-12-24 16:46:50

标签: php codeigniter

让我先描述一下我的代码。

查看:

<?php echo form_open_multipart('question_edit/update_question'); ?>
  ....
  <div class="form-group">
      <label for="ask_q" class="">A Brief Description of your question <em>(Optional)</em></label>
       <textarea name="ask_q" id="ask_q"><?php echo $uposts->question_desc; ?></textarea>
  </div>
  <div class="form-group">
      <label for="upld" class="">Upload New Docs <em>(Optional)</em></label>
      <input type="file" name="upld[]" id="upld" style="width: 100%;" multiple>
  </div>
  ....
<?php echo form_close(); ?>

请注意,我的文件输入字段不是必需的。它是可选

控制器:

function update_question(){

    $update_data = array(
        'question_desc'   =>  $this->input->post('ask_q')
    );

    $this->ask_model->update_q($update_data);

    if(!empty($_FILES['upld']['name'])){
        $filesCount = count($_FILES['upld']['name']);
        for($i = 0; $i < $filesCount; $i++){
            $_FILES['userFile']['name'] = $_FILES['upld']['name'][$i];
            $_FILES['userFile']['type'] = $_FILES['upld']['type'][$i];
            $_FILES['userFile']['tmp_name'] = $_FILES['upld']['tmp_name'][$i];
            $_FILES['userFile']['error'] = $_FILES['upld']['error'][$i];
            $_FILES['userFile']['size'] = $_FILES['upld']['size'][$i];

            $uploadPath = './uploads/';
            $config['upload_path'] = $uploadPath;
            $config['allowed_types'] = 'gif|jpg|jpeg|png|doc|docx|xls|xlsx|ppt|pptx|csv|ods|odt|odp|pdf|rtf|txt';
            $config['max_size'] = '1048576';

            $this->upload->initialize($config);

            if(!$this->upload->do_upload('userFile')){
                $this->session->set_flashdata('q_failure', 'Something went wrong. Please try again.');
                redirect('/user-qa');

            } else {
                $fileData = $this->upload->data();
                $uploadData[$i]['uploaded_file'] = $fileData['file_name'];

            }
        }

        if(!empty($uploadData)){
            //Insert files data into the database
            $insert = $this->ask_model->insert_upload($uploadData);
        }
    }

    $this->session->set_flashdata('q_success', '<div class="alert-message success">
            <i class="icon-ok"></i>
            <p><span>Success</span><br>
            Your question has been updated successfully.</p>
        </div>');
    redirect('/user-dashboard');

}

我的控制器代码描述(?)如果文件上传字段不为空,则上传文件。

但问题是我每次提交表单时都会因为$this->ask_model->update_q($update_data);更新数据库,但会提供我用于验证Something went wrong. Please try again.的错误消息$config

我检查了日志文件。日志文件状态

You did not select a file to upload

看来,我的if(!empty($_FILES['upld']['name'])){无效。

UPDATE-1

我尝试了if(isset($_FILES['upld']) && $_FILES['upld']['size'] > 0){而不是if(!empty($_FILES['upld']['name'])){,但结果是一样的。

我的问题是

  1. 问题出在哪里?
  2. 如何验证文件大小文件扩展名类型
  3. UPDATE-2

    通过查看此stackoverflow Link来尝试if($_FILES['upld']['name']){,但它也无效。

1 个答案:

答案 0 :(得分:0)

原因是当您提交一个空的多重表单时,它会发送索引为0的空数组 因此我们确保第一个索引不为空

if(isset($_FILES['files']['name'])&&!empty($_FILES['files']['name'][0])):