我正在一个项目中工作,我需要将多个csv文件的数据上传到数据库中的单个表。 目前我已编写代码,我上传包含csv文件的zip文件并将其解压缩到临时文件夹中。但我无法编写代码来读取每个csv文件并将数据插入到mysql中。 请指导我。
这是我的控制器代码
public function upload() {
$spcode = $this->input->post("spcode");
$config['upload_path'] = './adsl/';
$config['allowed_types'] = 'zip';
$data['msg'] = '';
$this->load->library('upload', $config);
// If upload failed, display error
if (!$this->upload->do_upload()) {
$data['msg'] = $this->upload->display_errors();
}
$this->load->view('templates/header');
$this->load->view('upload_wholesale', $data);
$this->load->view('templates/footer');
}
public function fetchFilesFromDirectory() {
$dir = "./adsl/";
$filesArr = array_diff(scandir($dir), array('..', '.'));
if(!empty($filesArr)) {
$zip = new ZipArchive;
foreach($filesArr as $fileName) {
$file = "./adsl/".$fileName;
chmod($file,0777);
if ($zip->open($file) === TRUE) {
$zip->extractTo('./wholesaleUploads/');
$zip->close();
$chunkFile = explode(".", $fileName);
$extracted_files = directory_map("./wholesaleUploads/".$chunkFile[0]);
chmod($extracted_files,0777);
if ($this->csvimport->get_array($extracted_files)) {
$csv_array = $this->csvimport->get_array($extracted_files);
//print_r($csv_array);
if(!empty($csv_array)) {
foreach ($csv_array as $row) {
$insert_data = array(
'accountNumber'=>$row['AccountNumber'],
'custId'=>$row['CustID'],
'serviceNumber'=>$row['ServiceNumber'],
'serviceType'=>$row['Service Type'],
'chargeType'=>$row['ChargeType'],
'details'=>$row['Details'],
'dateFrom'=>$row['DateFrom'],
'dateTo'=>$row['DateTo'],
'charge'=>$row['Charge'],
'gst'=>$row['GST'],
'payment'=>$row['Payment'],
'invoiceNumber'=>$row['InvoiceNumber'],
'spcode'=>$spcode,
);
$this->upload_adsl->insert_csv($insert_data);
}
}
$this->upload_adsl->addFiles($chunkFile[0].".csv");
unlink($file); // Remove from Resource Directory.
unlink($extracted_files); // Remove from Upload Directory.
echo "File uploaded successfully.";
}
}
}
}
}
}