我正在为销售公司开发一个网站。我想添加员工的批量数据,这将是一个CSV文件。我希望得到帮助。代码示例将非常受欢迎,因为我是新手。此代码应首先上载CSV,然后将其导入MySQL。提前谢谢!
答案 0 :(得分:1)
拥有一个带文件上传表单的视图,然后拥有一个带有控制器调用的上传功能的csv_model。这个函数应该包含看起来像这样的代码:
if (isset($_FILES['userfile']) && $_FILES['userfile']['size'] > 0 && $_FILES['userfile']['error'] == 0) {
// Upload the file:
if (!is_dir($this->upload_path)) mkdir($this->upload_path,0777,TRUE);
$config['upload_path'] = $this->upload_path;
$config['allowed_types'] = 'csv';
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload())
{
$error = array('error' => $this->upload->display_errors());
echo 'Error: '; print_r($error); die();
return $error;
}
else
{
// It's uploaded, so open it, loop through it and do what you need to do
$data = array('upload_data' => $this->upload->data());
$file_path = $data['upload_data']['full_path'];
$row = 1;
$db_row = array();
if (($handle = fopen($file_path, "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$num = count($data);
$db_row[$row]['xxx'] = $data[0];
// Any database insertion goes here...
}
}
}}