Answer the question
In order to leave comments, you need to log in
When generating a CSV file, umlauts are not displayed correctly, how to fix it?
Hello uv. community. Tell me, please, when generating a CSV file, umlauts are not displayed correctly, how to fix it?
The code:
define("DB_HOST", "localhost");
define("DB_LOGIN", "test");
define("DB_PASSWORD", "test");
define("DB_NAME", "test");
$db = @mysql_connect(DB_HOST, DB_LOGIN, DB_PASSWORD) or die("Ошибка соединения с сервером баз данных");
mysql_query('SET NAMES utf8');
mysql_select_db(DB_NAME) or die(mysql_error());
$result = array();
while ($row = @mysql_fetch_assoc( mysql_query("SELECT id, name FROM orders "))) {
$result[] = $row;
}
$arr = array();
$i = 1;
foreach($result as $row){
$arr += array($i => array(
"one" => $row["id"],
"two" => $row["name"]
));
$i++;
}
$f = fopen('test.csv', 'w');
foreach ($arr as $item) {
fputcsv($f, $item, ';');
}
fclose($f);
$filename = "test.csv";
// нужен для Internet Explorer, иначе Content-Disposition игнорируется
if(ini_get('zlib.output_compression'))
ini_set('zlib.output_compression', 'Off');
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false); // нужен для некоторых браузеров
header("Content-Type: application/force-download");
header("Content-Disposition: attachment; filename=\"".basename($filename)."\";" );
header("Content-Transfer-Encoding: binary");
readfile("$filename");
exit();
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question