Skip to content

Instantly share code, notes, and snippets.

@julienhay
Last active January 2, 2016 16:59
Show Gist options
  • Save julienhay/8334125 to your computer and use it in GitHub Desktop.
Save julienhay/8334125 to your computer and use it in GitHub Desktop.
Fonction Import csv (à adapter)
<?php
function importCSV() {
ini_set('auto_detect_line_endings',TRUE);
if ($this->request->is('post')) {
$dataToSave = array();
$row = 1;
if($this->data['Contacts']['csv_file']['type'] == "text/csv")
{
if (($handle = fopen($this->data['Contacts']['csv_file']['tmp_name'], "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ";")) !== FALSE)
{
$num = count($data);
if($row != 1)
{
$tmp = array();
$tmp['enterprise_id'] = $this->data['Contacts']['enterprise_id'];
$tmp['civility'] = $data[0];
$tmp['lastname'] = $data[1];
$tmp['firstname'] = $data[2];
$tmp['email'] = $data[3];
$tmp['address'] = $data[4];
$tmp['function'] = $data[5];
$tmp['company'] = $data[6];
$EmailSearch = $this->Contact->find('first', array(
'conditions' => array('Contact.email' => $data[3])
));
$this->Contact->create();
if(!empty($EmailSearch)) $this->Contact->id = $EmailSearch['Contact']['id'];
$this->Contact->save($tmp);
}
$row++;
}
fclose($handle);
}
$this->Session->setFlash(__('Data import correct'),'default',array('class' => 'success'));
}
else {
$this->Session->setFlash(__('Only CSV file please'),'default',array('class' => 'error'));
}
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment