Reputation: 53
I need to download csv file from export
folder which is inside the www
folder.
I could make it as link, but by attempting to save as the csv files, an empty csv file will be downloaded. Any idea?
<?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);
?>
by right clicking and selecting save link as, an empty csv file is downloaded
Upvotes: 0
Views: 3270
Reputation: 957
Try this one which i used this. Ex: 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;
}
}
?>
I used a link like this
$link = "<a href='download.php?link=$filename'> $filename </a><br />";
Upvotes: 1