我正在使用Codeigniter,我正在尝试将多个文件上传到数据库中。 这些文件是单独的字段,而不是一个字段的多个文件。
我关注了这篇文章 - http://codeigniter.com/forums/viewthread/110130/P0/
我几乎让它工作但我在将上传的数据添加到我的数据库时遇到了一些麻烦。
这是我的控制器功能......
function addStation(){
// set common properties
$data['title'] = 'Add new station';
$data['action'] = site_url('admin/station/addStation');
$data['link_back'] = anchor('admin/station/index/','Back to list of stations',array('class'=>'back'));
// set validation properties
$this->_set_fields();
$this->_set_rules();
$config['allowed_types'] = 'pdf|xls|doc|docx';
$config['max_size'] = '1000';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$path_to_uploads='./uploads/files/stations';
$config['upload_path'] = $path_to_uploads;
// File specific (overrule global and optional)
$config['logo_file']['upload_path'] = './uploads/images/stations';
$config['logo_file']['allowed_types'] = 'gif|jpg|png';
$this->load->library('upload', $config);
//add this
$this->upload->initialize($config);
if (!$this->upload->do_upload(array('rajar_file','playlist_file','map_file', 'logo_file'))){
$error = $this->upload->display_errors();
echo "<script>alert($error);</script>";
}else{
$data = array('upload_data' => $this->upload->data(array('rajar_file','playlist_file','map_file', 'logo_file')));
$rajar_file_name = $upload_data['rajar_file']['file_name'];
$rajar_full_file_path = $path_to_uploads.'/'.$rajar_file_name;
$playlist_file_name = $upload_data['playlist_file']['file_name'];
$playlist_full_file_path = $path_to_uploads.'/'.$playlist_file_name;
$map_file_name = $upload_data['map_file']['file_name'];
$map_full_file_path = $path_to_uploads.'/'.$map_file_name;
$logo_file_name = $upload_data['logo_file']['file_name'];
$logo_full_file_path = $path_to_uploads.'/'.$logo_file_name;
}
// run validation
if ($this->validation->run() == FALSE){
$data['message'] = '';
}else{
// save data
$station = array('name' => $this->input->post('name'),
'rajar' => $rajar_full_file_path,
'playlist' => $playlist_full_file_path,
'map' => $map_full_file_path,
'logo' => $logo_full_file_path );
$id = $this->stationModel->save($station);
// set form input name="id"
$this->validation->id = $id;
// set user message
$data['message'] = '<div class="success">add new station success</div>';
}
$this->template->set('title', 'Admin - Add New Station!');
$this->template->load('admin/template', 'admin/stationEdit', $data);
}
提交表单后,我收到以下错误......
消息:未定义的变量:rajar_full_file_path
消息:未定义的变量:playlist_full_file_path
消息:未定义的变量:map_full_file_path
消息:未定义的变量:logo_full_file_path
我是愚蠢的,我无法理解?
由于 丹
答案 0 :(得分:1)
我似乎文件上传失败,因此没有设置这些变量。显示上载错误的方法可能不起作用。请尝试将错误发送回视图:
而不是:
$error = $this->upload->display_errors();
echo "<script>alert($error);</script>";
尝试:
$error = $this->upload->display_errors();
$data['message'] = $error;
答案 1 :(得分:1)
似乎您的代码正在输入第一个if语句,然后传递表单验证(因此从不定义您的变量),然后尝试在第二个其他代码块中使用这些变量。