Codeigniter上传PDF问题 [英] Codeigniter upload PDF problem

查看:126
本文介绍了Codeigniter上传PDF问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用codeigniter上传库上载PDF,如果用户正在添加新闻,则上传PDF,同样的表单用于添加新文章。这两者之间的区别在于新闻稿需要PDF,但是新闻文章没有。然而,当我提交我的表单而没有将PDF添加到上传字段时,我得到一个错误


您没有选择要上传的文件


我怎样才能做到这一点,所以我没有上传文件?

  function add() 
{

$ data = array();
//设置一些验证规则

$ 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');
如果($这 - > form_validation->运行()== FALSE)
{
$这 - >负载>查看( '模板/ admin_header.php', $的数据);
$ this-> load-> view('admin / news / add');
$ this-> load-> view('templates / footer.php');
}
else
{
//验证已通过,我们保存数据,如果存在数据,我们可以上传PDF
($ this- > input-> post('userfile')!=)
{
$ config ['upload_path'] ='./assets/doc/';
$ config ['allowed_types'] ='pdf';
$ config ['max_size'] ='5000';

$ this-> load->库('upload',$ config);
$ b $ if if(!$ this-> upload-> do_upload())
{
//如果我们要显示一个问题,请尝试上传表单和错误
$ 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');




// everyhting已经很好了文件已经上传,所以我们可以继续和创建一个POST数据数组和文件名
//保存在数据库中。
// $ file = $ this-> upload-> data();
$ insertData =阵列(
'标题'=> $这 - >输入 - >柱( '标题'),
'标语'=> $这 - >输入 - >后('标语'),
//'pdf'=> // $ file ['file_name'],
'article'=> $ this-> input- >帖子('article')
);

$ this-> load-> model('newsArticles');
$ this-> session-> set_flashdata('sucess','您的新闻文章已成功保存');
重定向('/ admin / dashboard','重定向');
}

}



}


解决方案

错误只是说你没有选择一个文件,你没有。当我这样做,我只是忽略然后错误,并继续前进。



但有点更整洁的方式将使用类似:



'pre> 如果(isset($ _ FILES [ 'userfile的'])){
//上传文件
$这 - > upload-> ; do_upload();
//检查错误等
} else {
//什么也不做
}


I am uploading a PDF using the codeigniter upload library, the PDF is uploaded if the user is adding a press, the same form is used to add a new article. This difference between the two is that a press release needs a PDF, but a news article does not.

However when I submit my form without add PDF to the upload field, I get an error

you did not select a file to upload

How can I make it so I don't have upload a file?

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');
        }

    }



}

解决方案

The error just says you didn't pick a file, which you didn't. When I do this, I just ignore then error and keep going.

but a bit tidier way would be to use something like:

if (isset($_FILES['userfile'])) {
    // upload the file 
    $this->upload->do_upload();
    // check errors etc
} else {
    // do nothing
}

这篇关于Codeigniter上传PDF问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆