[英]How to download a specific CSV file from the server to the client using Save As dialog box in php?
I have a file named CO2.csv on the server and I want the user through a button in a form to download and save it with a new file name to a whatever location in his hard drive. 我在服务器上有一个名为CO2.csv的文件,我希望用户通过表单中的按钮下载并使用新文件名将其保存到他的硬盘驱动器中的任何位置。
this is the Html script of the button that calls the php code 这是调用php代码的按钮的HTML脚本
<form action="FCO2.php" method="post" <?php if(empty($_SESSION["CO2"])){?>style="display:none"<?php } ?> >
<input type="submit" value="Export to CSV" name="submit">
</form>
And here is the FCO2.php 这是FCO2.php
<?php
$file = 'temp/CO2temp.csv';
if(!$file)
{ // file does not exist
die('file not found');
}
else
{
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename=$file");
header("Content-Type: text/csv");
header("Content-Transfer-Encoding: text/csv");
}
?>
But it didn't work, it always create an empty file and never copies the file that is located on the server. 但是它没有用,它总是创建一个空文件,并且从不复制服务器上的文件。
oh your just missing a call to the actual file such as using readfile
哦,您只是错过了对实际文件的调用,例如使用readfile
so adding it in: 因此添加到:
<?php
$file = 'temp/CO2temp.csv';
if(!$file)
{ // file does not exist
die('file not found');
}
else
{
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename=$file");
header("Content-Type: text/csv");
header("Content-Transfer-Encoding: text/csv");
readfile($file);
exit;// good practice but if this is the whole file, its not needed
}
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.