【问题标题】:PHP add button to download csv filePHP添加按钮下载csv文件
【发布时间】:2020-10-15 10:38:11
【问题描述】:
<?php
$name = 't1__'.date('m-d-Y_hia__').$_GET["ident"].'.txt';
$myfile = fopen($name, "w") or die("Unable to open file!");
fwrite($myfile, $_GET["value"]);
fclose($myfile);
?>

嗨!我需要帮助!

所以.. 我有一个文件 axWrite.php,它将从我创建的基于 Web 的研究中获取值,但我想添加一个下载按钮,以便在打开每个文件时获取包含内容的 csv 文件。

例如,从这张图片中:Picture of the files created from axWrite.php我希望能够在单击链接或输入文件时下载,我可以在页面Inside the page with the get values内有一个按钮下载到csv文件/p>

我试过了:

$filename = $name.".csv";
header("Content-Disposition: attachment; filename=\"$filename\"");
header("Content-Type: text/csv; charset=UTF-16LE");
$out = fopen("php://output",'w');

但它没有做任何事情,因为它只在我点击 axWrite.php 时创建一个空白页面。

任何帮助都会有所帮助!

谢谢,

【问题讨论】:

标签: php csv download


【解决方案1】:

您的链接或按钮需要使用您的 PHP 函数。尝试一下,您可以在链接或按钮之间进行选择。您可以在echo + file_get_contentsreadfile 之间进行选择。我不太记得了,但这个想法是这样的。

/* html - 2 alternatives */ 
<button type="button" onclick="location.href='your-file.php?params=some'">Your text</button>
<a href="your-file.php?params=some">Your text</a>



/* php echo or readfile */
$filename = $name.".csv";
header("Content-Type: text/csv; charset=UTF-16LE");
header("Content-Disposition: attachment;filename=$filename");
echo file_get_contents($filename);
readfile($filename);
exit()

【讨论】:

    猜你喜欢
    • 2019-04-29
    • 2018-04-06
    • 2015-07-13
    • 2019-01-10
    • 1970-01-01
    • 2017-03-05
    • 2021-10-11
    • 2015-06-04
    • 1970-01-01
    相关资源
    最近更新 更多