【发布时间】:2011-08-04 19:34:36
【问题描述】:
我正在使用 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');
}
}
}
【问题讨论】:
标签: php codeigniter file-upload upload