CSV — PHP файл загрузки в метод не работает

У меня есть класс php с создателем CSV и загрузчик файлов, который должен загрузить файл после создания CSV. Создатель CSV работает, а загрузчик файлов — нет. Не могу понять, почему его вообще не скачивают.

Код ниже:

class Orders
{
public $pdoLink,
$timestamp;public function __construct()
{
include_once "connect.php";
$this->pdoLink = $pdo;
$this->timestamp = date("Y-m-d-h_i_s_a",time());
}

public function createCsv($begindate = false, $endDate = false)
{

$list = (($begindate) && ($endDate)) ? $this->getOrders($begindate,$endDate) : $this->getOrders();

$file_name = "orders_" . $this->timestamp . ".csv";
$file = fopen($file_name,"w");

foreach ($list as $line)
{
fputcsv($file,explode(',',$line));
}
$this->downloadCsv($file_name);

}
public function downloadCsv($file)
{
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.urlencode($file));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
ob_clean();
flush();
readfile($file);
exit;
}
}

0

Решение

Просто попробуйте этот код

$filename = "file.csv";
$fp = fopen('php://output', 'w');
header('Content-type: application/csv');
header('Content-Disposition: attachment; filename='.$filename);
fputcsv($fp, $header_data);
fclose($fp);
exit;
0

Другие решения

Попробуй это

$file = urlencode($file);
header('Content-Type: text/csv; charset=utf-8');
header("Content-Disposition: attachment; filename=\"$file\"");
0

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector