微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Codeigniter上传PDF问题

如何解决Codeigniter上传PDF问题

| 我正在使用codeigniter上载库上载PDF,如果用户添加印刷机,则会上载PDF,使用相同的表单来添加文章。两者之间的区别在于新闻稿需要PDF,而新闻文章则不需要。 但是,当我提交表单而未在上传字段中添加PDF时,出现错误   您没有选择要上传文件 我该如何做才能没有上传文件
function add ()
{

    $data = array();
    //set some validation rules

    $this->form_validation->set_rules(\'headline\',\'headline\',\'required|trim\');
    $this->form_validation->set_rules(\'tagline\',\'tagline\',\'trim\');
    $this->form_validation->set_rules(\'userfile\',\'userfile\',\'trim\');
    $this->form_validation->set_rules(\'article\',\'article\',\'required|trim|htmlspecialchars\');
    if ($this->form_validation->run() == FALSE)
    {
        $this->load->view(\'templates/admin_header.PHP\',$data);
        $this->load->view(\'admin/news/add\');
        $this->load->view(\'templates/footer.PHP\');
    }
    else
    {
        //validation has been passed and we save the data and if there is one present we can upload the PDF
        if($this->input->post(\'userfile\') != \"\")
        {
            $config[\'upload_path\'] = \'./assets/doc/\';
            $config[\'allowed_types\'] = \'pdf\';
            $config[\'max_size\'] = \'5000\';

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

            if(!$this->upload->do_upload())
            {
                //try and do the upload if there is a problem we are going to show the form and an error
                $data[\'error\'] = array(\'upload_error\' => $this->upload->display_errors()); 
                //die(print_r($data,\'upload\'));
                $this->load->view(\'templates/admin_header.PHP\',$data);
                $this->load->view(\'admin/news/add\',$data);
                $this->load->view(\'templates/footer.PHP\');
            }
        }
        else
        {
            //everyhting has gone well the file has been upload so we can carry on and create an array of the POST data and the filename
            //to save in the database.
            //$file = $this->upload->data();
            $insertData = array(
                \'headline\' => $this->input->post(\'headline\'),\'tagline\' => $this->input->post(\'tagline\'),//\'pdf\' => //$file[\'file_name\'],\'article\' => $this->input->post(\'article\')
            );

            $this->load->model(\'newsArticles\');
            $this->session->set_flashdata(\'sucess\',\'Your news article was successfully saved\');
            redirect(\'/admin/dashboard\',\'redirect\');
        }

    }



}
    

解决方法

该错误只是表明您没有选择文件,而您没有。当我这样做时,我只会忽略错误然后继续前进。 但更简洁的方法是使用类似:
if (isset($_FILES[\'userfile\'])) {
    // upload the file 
    $this->upload->do_upload();
    // check errors etc
} else {
    // do nothing
}
    ,似乎您的第二个ѭ2outside在
if(!do_upload)
之外,因此您尚未完成功能。这是CI用户指南中的示例上传脚本:
function do_upload()
{
    $config[\'upload_path\'] = \'./uploads/\';
    $config[\'allowed_types\'] = \'gif|jpg|png\';
    $config[\'max_size\'] = \'100\';
    $config[\'max_width\']  = \'1024\';
    $config[\'max_height\']  = \'768\';

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

    if ( ! $this->upload->do_upload())
    {
        $error = array(\'error\' => $this->upload->display_errors());

        $this->load->view(\'upload_form\',$error);
    }
    else
    {
        $data = array(\'upload_data\' => $this->upload->data());

        $this->load->view(\'upload_success\',$data);
    }
}
只需在
do_upload
if
之前用
if
检查上传文件     

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。