gpt4 book ai didi

PHP - 将 CSV 文件流式传输到浏览器

转载 作者:行者123 更新时间:2023-12-05 09:20:43 50 4
gpt4 key购买 nike

我在将 CSV 文件直接下载到我的浏览器而不将文件存储在我的服务器上的某个位置时遇到问题。我找到了一个教程 ( https://www.perpetual-beta.org/weblog/php-stream-file-direct.html ),但我似乎无法使用它!

这是我的主要功能:

public function exportRecord($id) {
$contest = (string) $this->getContest($id);
$entries = (array) $this->getEntries($id);

$filename = sprintf('%1$s-%2$s-%3$s', str_replace(' ', '', $contest['name']), date('Ymd'), date('His'));
$output = fopen('php://output', 'w');

ob_start();

$header = array(
'First Name',
'Last Name',
'Address',
'City',
'State',
'Zip',
'Phone',
'Email',
'Item Purchased',
'Partner Name',
'Partner #',
'Date Entered',
'Subscribe'
);

fputcsv($output, $header);

foreach ($entries as $entry) {
fputcsv($output, $entry);
}

$string = ob_get_clean();

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/octet-stream');
header('Content-Disposition: attachment; filename="' . $filename . '.csv";');
header('Content-Transfer-Encoding: binary');

exit($string);
}

我正在调用以下两个函数,它们只是对我的数据库进行查询...

private function getContest($id) {
$query = sprintf(
'SELECT name ' .
'FROM contests ' .
'WHERE id = %s;',
$id
);

$result = $this->mysql->query($query);
$response = '';

if ($result) {
while ($row = mysqli_fetch_assoc($result)) {
$response = $row;
}
} else {
echo $this->mysql->error;
}

return $response;
}

private function getEntries($id) {
$query = sprintf(
'SELECT firstName, ' .
'lastName, ' .
'CONCAT(address1, ", ", address2), ' .
'city, ' .
'state, ' .
'zip, ' .
'phone, ' .
'email, ' .
'itemPurchased, ' .
'partnerName, ' .
'partnerNum, ' .
'dateEntered, ' .
'subscribe ' .
'FROM entries ' .
'WHERE contestID = %s;',
$id
);

$result = $this->mysql->query($query);
$response = '';

if ($result) {
while ($row = mysqli_fetch_assoc($result)) {
$response[] = $row;
}
} else {
echo $this->mysql->error;
}

return $response;
}

非常感谢您提供的任何帮助/建议!这是一个屏幕截图,只是为了让您了解问题所在: Screenshot谢谢!

最佳答案

您缺少 Jonathan Hollin 示例中的几个步骤。您需要开始输出缓冲,您必须写出缓冲区并正确设置 header 。这可能需要一些修改,但我尝试使示例适应您在此问题中提供的内容:

<?php
public function exportRecord($id) {
$contest = (array) $this->getContest($id);
$entries = (array) $this->getEntries($id);

$filename = sprintf('%1$s-%2$s-%3$s', str_replace(' ', '', $contest['name']), date('Ymd'), date('His'));

$header = array(
'First Name',
'Last Name',
'Address',
'City',
'State',
'Zip',
'Phone',
'Email',
'Item Purchased',
'Partner Name',
'Partner #',
'Date Entered',
'Subscribe'
);

export_csv($header, $entries, $filename);
}

private function export_csv($header, $data, $filename) {
// No point in creating the export file on the file-system. We'll stream
// it straight to the browser. Much nicer.

// Open the output stream
$fh = fopen('php://output', 'w');

// Start output buffering (to capture stream contents)
ob_start();

// CSV Header
if(is_array($header)){
fputcsv($fh, $header);
}

// CSV Data
foreach ($data as $row) {
fputcsv($fh, $row);
}

// Get the contents of the output buffer
$string = ob_get_clean();

// Output CSV-specific headers
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/octet-stream');
header('Content-Disposition: attachment; filename="' . $filename . '.csv";');
header('Content-Transfer-Encoding: binary');

// Stream the CSV data
exit($string);
}

关于PHP - 将 CSV 文件流式传输到浏览器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36558811/

50 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com