繁体   English   中英

使用php从文件夹下载文件

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM