Мне нужна небольшая помощь здесь.
Я намеревался создать 2 файла CSV в PHP и загрузить их браузером.
Это произойдет, когда пользователи нажмут кнопку. Однако я обнаружил, что для каждого http-запроса разрешена только 1 загрузка файла. Я наткнулся на этот пост * * * * * * * * * Создайте несколько файлов CSV в памяти, а затем сожмите . Решение создает 3 файла CSV в памяти, добавляет их в zip-файл и загружает в браузер клиента.
$headers = array('id', 'name', 'age', 'species');
$records = array(
array('1', 'gise', '4', 'cat'),
array('2', 'hek2mgl', '36', 'human')
);
// create your zip file
$zipname = 'file.zip';
$zip = new ZipArchive;
$zip->open($zipname, ZipArchive::CREATE);
// loop to create 3 csv files
for ($i = 0; $i < 3; $i++) {
// create a temporary file
$fd = fopen('php://temp/maxmemory:1048576', 'w');
if (false === $fd) {
die('Failed to create temporary file');
}
// write the data to csv
fputcsv($fd, $headers);
foreach($records as $record) {
fputcsv($fd, $record);
}
// return to the start of the stream
rewind($fd);
// add the in-memory file to the archive, giving a name
$zip->addFromString('file-'.$i.'.csv', stream_get_contents($fd) );
//close the file
fclose($fd);
}
// close the archive
$zip->close();
header('Content-Type: application/zip');
header('Content-disposition: attachment; filename='.$zipname);
header('Content-Length: ' . filesize($zipname));
readfile($zipname);
// remove the zip archive
unlink($zipname);
Я попробовал решение, помеченное как правильное, но оно не сработало.
Файл zip загружен, но он пуст.
![var_dump($zip) shows that there are 3 files.](https://i.stack.imgur.com/Vsh3S.png)
Любая помощь будет оценена.
PS: Извините, плохой английский, я не являюсь носителем языка.