我正在尝试使用 csv 插入记录。我想将 csv 上传到我的应用程序并希望将其导入数据库。现在我有一个用户表,所以我想通过导入用户的 csv 文件来创建用户。我对文件上传了解一点,但对将其导入数据库一无所知,请帮助。
请参阅我已使用以下命令完成文件上传:
控制器:
public function upload_csv()
{
$this->load->helper('form');
$this->load->helper('url');
//Set the message for the first time
$data = array('msg' => "Upload File");
$data['upload_data'] = '';
//load the view/upload.php with $data
$this->load->view('admin/user/upload', $data);
}
另一个控制器Upload_file
public function upload_it() {
//load the helper
$this->load->helper('form');
//Configure
//set the path where the files uploaded will be copied. NOTE if using linux, set the folder to permission 777
$config['upload_path'] = 'application/views/uploads/';
// set the filter image types
$config['allowed_types'] = 'gif|csv';
//load the upload library
$this->load->library('upload', $config);
$this->upload->initialize($config);
$this->upload->set_allowed_types('*');
$data['upload_data'] = '';
//if not successful, set the error message
if (!$this->upload->do_upload('userfile')) {
$data = array('msg' => $this->upload->display_errors());
} else { //else, set the success message
$data = array('msg' => "Upload success!");
$data['upload_data'] = $this->upload->data();
}
//load the view/upload.php
$this->load->view('admin/user/upload', $data);
}
和观点:
<code>
<?php if($upload_data != ''):?>
<?php var_dump($upload_data);?>
</code>
<img scr="<?php echo $upload_data['full_path'];?>">
<?php endif;?>
<?php echo form_open_multipart('admin/upload_file/upload_it');?>
<input type="file" name="userfile" size="20" />
<br /><br />
<input type="submit" value="upload" />
</form>
现在如何将上传的文件导入我的数据库?
来自 2 秒的 Google 和PHP docs http://php.net/manual/en/function.str-getcsv.php:
$csv = array_map('str_getcsv', file('data.csv'));
然后你可以循环遍历数组并UPDATE
your DB
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)