开发者

Codeigniter upload PDF problem

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' =&开发者_如何学运维gt; $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
}


it looks like your second }else{ is outside the if(!do_upload) so your not completing your function. Here's an example upload script from CI user guide:

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

just check the upload file with if before the do_upload if

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜