
[英]Why does PHP-generated content jitter when loaded trough jQuery, and how do I fix it?
[英]How do I prompt the user to download a PHP-generated .CSV file, and how should I handle deleting it?
这是我的PHP代码,用于根据某些SQL数据创建.CSV文件。 它按预期工作,唯一的问题是它只是在服务器上创建一个.CSV文件,并不会提示用户下载它 。
<?php
require_once "../assets/repository/mysql.php";
$query = "SELECT * FROM datashep_AMS.COMPLETE_APPLICATIONS";
$results = mysql_query($query);
$first = true;
$out = fopen('export.csv', 'w');
while($row = mysql_fetch_assoc($results)){
if($first){
$titles = array();
foreach($row as $key=>$val){
$titles[] = $key;
}
fputcsv($out, $titles);
$first = false;
}
fputcsv($out, $row);
}
fclose($out);
?>
所以我的问题是,如何让用户在生成文件后立即下载文件?
并且,一旦他们下载(或拒绝),我该如何处理从我的服务器删除.CSV文件?
无需在服务器上存储任何内容(因此无需删除...)。 只需将结果写回浏览器并相应地设置标题:
<?php
require_once "../assets/repository/mysql.php";
$query = "SELECT * FROM datashep_AMS.COMPLETE_APPLICATIONS";
$results = mysql_query($query);
$first = true;
header('Content-Type: text/csv');
header('Content-Disposition: attachment;filename="export.csv"');
header('Cache-Control: max-age=0');
$out = fopen('php://output', 'w');
while($row = mysql_fetch_assoc($results)){
if($first){
$titles = array();
foreach($row as $key=>$val){
$titles[] = $key;
}
fputcsv($out, $titles);
$first = false;
}
fputcsv($out, $row);
}
fclose($out);
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.