且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

php下载文件:header()

更新时间:2023-12-04 20:53:52

Below script will help you download the file created

//Below is where you create particular month's text file
$file=$month . '.txt';
$handle=fopen($file, "w");
while ($row=mysql_fetch_array($res)){
    $writestring = $row['data_I_want'] . "\r\n";
    fwrite($handle, $writestring);
}
fclose($handle);
//Now the file is ready with data from database

//Add below to download the text file created
$filename = $file; //name of the file
$filepath = $file; //location of the file. I have put $file since your file is create on the same folder where this script is
header("Cache-control: private");
header("Content-type: application/force-download");
header("Content-transfer-encoding: binary\n");
header("Content-disposition: attachment; filename=\"$filename\"");
header("Content-Length: ".filesize($filepath));
readfile($filepath);
exit;

相关阅读

推荐文章