I want to read a csv file and put the data in a mysql database. The problem is that the "é" is badly encoded. I try to put this header :
header('Content-Type: text/html; charset=UTF-8');
But It didn't work.
Here is my code, the problème is on the variable $site :
<?php
include_once __DIR__ . '/../../../components/functions.php';
$filename = "../../../Factures/factures.csv";
header('Content-Type: text/html; charset=UTF-8');
if(($csvfile = fopen($filename, "r")) !== FALSE)
{
fgets($csvfile); fgets($csvfile); fgets($csvfile); fgets($csvfile);
while (($data = fgetcsv($csvfile,0, ",")) !== FALSE)
{
$data = array_map("utf8_encode", $data);
//Formattage des données
$j = substr($data[3],0,2); $m = substr($data[3], 3,2); $y = substr($data[3], 6, 4);
$dateDeb = $y . $m . $j;
$j2 = substr($data[4],0,2); $m2 = substr($data[4], 3,2); $y2 = substr($data[4], 6, 4);
$dateFin = $y2 . $m2 . $j2;
$data[5] = str_replace(",", ".", $data[5]);
$data[6] = str_replace(",", ".", $data[6]);
$data[8] = str_replace(",", ".", $data[8]);
print($data[0] . "\n");
$req = $pdo->prepare('INSERT INTO Factures SET Site = :unSite, Type = :unType, Numero = :unNumero, DateFacture = :uneDateFacture, DateEcheance = :uneDateEcheance, MontantHT = :unMontantHT, TVA = :uneTVA, MontantTT = :unMontantTT, SoldeTTC = :unSoldeTTC');
$req->execute(array(
':unSite' => utf8_encode($data[0]),
':unType' => $data[1],
':unNumero' => $data[2],
':uneDateFacture' => date("Y/m/d", strtotime($dateDeb)),
':uneDateEcheance' => date("Y/m/d", strtotime($dateFin)),
':unMontantHT' => round(doubleval($data[5]),2),
'uneTVA' => round(doubleval($data[6]),2),
'unMontantTT' => round(doubleval($data[5]+doubleval($data[6])),2),
'unSoldeTTC' => round(doubleval($data[8]),2)
));
}
fclose($csvfile);
//sup ou garder
}