使用php从文件夹下载文件

时间:2016-02-22 11:56:44

标签: php html

我需要从export文件夹中的www文件夹下载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);
?>

结果链接如下:
enter image description here

右键单击并选择保存链接,下载空的csv文件

1 个答案:

答案 0 :(得分:1)

试试我用过的这个。例如:download.php

[["val1", "1"], ["val2", "2"], ["val1", "3"], ["val3", "4"], ["val2", "5"], ["val2", "6"]]
.group_by(&:first).map{|k, a| [k, a.map(&:last)]}
# => [["val1", ["1", "3"]], ["val2", ["2", "5", "6"]], ["val3", ["4"]]]

我使用了像这样的链接

<?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;
    }
} 
?>