Php 从服务器下载CSV文件
我正在查询数据库并使用结果创建csv文件。然后,我在前端页面上添加了一个指向服务器上创建的文件的链接,如下所示:Php 从服务器下载CSV文件,php,csv,Php,Csv,我正在查询数据库并使用结果创建csv文件。然后,我在前端页面上添加了一个指向服务器上创建的文件的链接,如下所示: function writetofile($stringData, $myFile) { $fh = fopen('download/'.$myFile, 'w') or die("can't open file"); fwrite($fh, $stringData); fclose($fh); } $filename = $file."_".date
function writetofile($stringData, $myFile) {
$fh = fopen('download/'.$myFile, 'w') or die("can't open file");
fwrite($fh, $stringData);
fclose($fh);
}
$filename = $file."_".date("d-m-Y_H-i",time()).'.csv';
writetofile($seperator, $filename);
print 'Right-click on the link below and select "Save as":' . '<br/><br/>';
print 'Download File: <a href="/download/'.$filename.'" target="_blank">Download Link</a>';
函数writetofile($stringData,$myFile){
$fh=fopen('download/'。$myFile,'w')或die(“无法打开文件”);
fwrite($fh,$stringData);
fclose($fh);
}
$filename=$file.“u”.date(“d-m-Y_H-i”,time()).csv”;
writetofile($separator,$filename);
打印“右键单击下面的链接并选择“另存为”:”
;
打印“下载文件:”;
出于某种原因,当我直接从服务器下载文件时,它看起来与数据库中的所有行都是正确的。但是,当我完成用户将要执行的步骤时,即右键单击并保存,文件仅包含此页面中的html代码 更新查看 放置合适的php标题,输出文件内容并退出:
<?php // none space here
function writetofile($stringData, $myFile)
{
if ( ! file_exists('download/' . $myFile))
{
echo 'file missing';
}
else
{
header('HTTP/1.1 200 OK');
header('Cache-Control: no-cache, must-revalidate');
header("Pragma: no-cache");
header("Expires: 0");
header("Content-type: text/csv");
header("Content-Disposition: attachment; filename=$myFile");
readfile('download/' . $myFile);
exit;
}
}
如果您只需单击链接,而不是右键单击另存为怎么办?不需要target=“\u blank”
-尽管我怀疑这会解决您的问题吗?$separator
的值是多少?$file
的值是多少?(即,向我们展示所有相关代码)此脚本可能正在运行文档root?@mg1,那么您准备好了吗?我建议您从专用文件输出csv。。。(除了您链接它之外)