[英]download file from a folder using php
我需要从www
文件夹中的export
文件夹下载csv文件。
我可以将其作为链接,但是通过尝试另存为csv文件,将下载一个空的csv文件。 任何想法?
<?php
$dir_open = opendir('./export');
while(false !== ($filename = readdir($dir_open))){
if($filename != "." && $filename != ".."){
$link = "<a href='./$filename'> $filename </a><br />";
echo $link;
}
}
closedir($dir_open);
?>
通过右键单击并选择另存为,下载一个空的csv文件
试试这个我用过的。 例如:download.php
<?php
if(isset($_GET['link']))
{
$var_1 = $_GET['link'];
$dir = "export/";
$file = $dir . $var_1;
if (file_exists($file))
{
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($file));
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
ob_clean();
flush();
readfile($file);
exit;
}
}
?>
我使用了这样的链接
$link = "<a href='download.php?link=$filename'> $filename </a><br />";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.