上传codeIgniter中的两个文件,显示错误

时间:2013-11-25 10:22:08

标签: php codeigniter

我的视图表格多部分

<h3>Upload Submission File(doc,docx,pdf,cda,rtf,txt)</h3>
<?php echo form_error('fileToUpload'); ?>
<p>                     
<input type="file" name="fileToUpload" id="fileToUpload" title="Upload Submission File" 
       accept="application/pdf, .doc, .docx, .txt, application/msword, .rtf, .cda"/>
</p>

<h3>Upload Additional File (gif,jpeg,png,tiff,pdf)</h3>

<?php echo form_error('additionalUpload'); ?>
<p>                     
<input type="file" name="additionalUpload" id="additionalUpload" title="Upload Additional File" 
       accept="image/gif, image/jpeg, image/png, image/x-tiff, application/pdf" />
</p>

我的控制器

function submit_article() {
    $this->load->helper(array('form', 'url'));
    $this->load->library('form_validation');
    $this->form_validation->set_error_delimiters('<p style="color:red">', '<br/></p>');

    $my_rules = array(
        array(
            'field' => 'fileToUpload',
            'label' => 'Submission File',
            'rules' => 'callback_is_proper_file'
        ),
        array(
            'field' => 'additionalUpload',
            'label' => 'Additional File',
            'rules' => 'callback_is_image'
        )
    );

    $this->form_validation->set_rules($my_rules);
    if ($this->form_validation->run() == FALSE) {
        //ERROR
        $data['title'] = ucfirst('submit Article');
        $this->load->view('templates/header', $data);
        $this->load->view('submit_article', $data);
        $this->load->view('templates/footer', $data);
    } else {
        //SUCCESS

        $data['title'] = ucfirst('article Submitted');
        $this->load->view('templates/header', $data);
        $this->load->view('forms_view/submit_article_success', $data);
        $this->load->view('templates/footer', $data);
    }
}



function is_image() {
    if ($_FILES['additionalUpload']['tmp_name'] != '') {

        $config1['upload_path'] = './public/uploads/';
        $config1['allowed_types'] = 'gif|jpg|jpeg|png|pdf|tiff';
        $config1['max_size'] = '2048';
        $config1['max_width'] = '0';
        $config1['max_height'] = '0';
        $config1['remove_spaces'] = true;
        $config1['overwrite'] = true;

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

        if (!$this->upload->do_upload('additionalUpload')) {
            $this->form_validation->set_message('is_image', $this->upload->display_errors('<p style="color:red">', '<br/></p>'));
            return FALSE;
        } else {
            $this->upload->data();
            return TRUE;
        }
    }
}

function is_proper_file() {
    if ($_FILES['fileToUpload']['tmp_name'] != '') {
        $config['upload_path'] = './public/uploads/';
        $config['allowed_types'] = 'doc|docx|pdf|cda|rtf|txt';
        $config['max_size'] = '1024*10';
        $config['max_width'] = '0';
        $config['max_height'] = '0';
        $config['remove_spaces'] = true;
        $config['overwrite'] = true;

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

        if (!$this->upload->do_upload('fileToUpload')) {
            $this->form_validation->set_message('is_proper_file', $this->upload->display_errors('<p style="color:red">', '<br/></p>'));
            return FALSE;
        } else {
            $this->upload->data();
            return TRUE;
        }
    } else {
        $this->form_validation->set_message('is_proper_file', '<p style="color:red">Please select a file to upload.<br/></p>');
        return FALSE;
    }
}

additionalUpload字段不是必填字段,filetoupload是必填字段。 当我通过填写除extraUpload之外的所有细节提交表单时,我的代码对我来说效果很好,但是当我提交带有additionalUpload的表单以及其他详细信息时,它会显示错误“您尝试上传的文件类型是不允许的”。 for additionalUpload field ..

换句话说,当我提交一个文件是filetoupload的表格时,需要它对我有用..当我提交包含filetoUpload和additionalUpload文件的表格时,它会显示我的错误,以便额外上传字段“您正在尝试上传的文件类型是不允许的。”,当我只提交只有addtionalupload文件的表单时,它会在我的上传文件夹中上传但是当我提交所有字段时它会显示错误“文件类型你试图上传是不允许的。“对于额外的上传文件..

感谢先生,请帮助任何人

1 个答案:

答案 0 :(得分:0)

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

我需要像上面那样调用它,就像我用这种方式调用它一样:

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