Chapter 7: Improving the Dashboard
199
Figure 7-6
The next part is where you start using the CSVReader library. You ’ re going to add an importCsv()
function to the MProducts model. In this function, you ’ re going to load two libraries: upload and
CSVReader. You ’ re going to upload the CSV files into the /tmp directory and use the parseFile()
method to parse the document and return an array of data.
function importCsv(){
$config[‘upload_path’] = ‘/tmp/’;
$config[‘allowed_types’] = ‘csv’;
$config[‘max_size’] = ‘2000’;
$config[‘remove_spaces’] = true;
$config[‘overwrite’] = true;
$this- > load- > library(‘upload’, $config);
$this- > load- > library(‘CSVreader’);
if(!$this- > upload- > do_upload(‘csvfile’)){
$this- > upload- > display_errors();
exit();
}
$csv = $this- > upload- > data();
$path = $csv[‘full_path’];
return $this- > csvreader- > parseFile($path);
}