PHP 下载保存文件到本地
经常需要点击按钮,然后弹出一个对话框,保存下载文件。
最常见的方式,就用<a>链接实现,例如:
<a href=”xxx/youfile.txt”> youfile.txt </a>
本文介绍的下载保存方式,是通过生成文件后,然后用代码实现下载保存。
完整示例(推荐)
<?php
/**
* 下载文件header函数
* copyright by www.mimvp.com
* 2015-05-10
*/
$res_filepath = "";
if(isset($_GET["filepath"])) {
$res_filepath = $_GET["filepath"];
}
// $filepath = "./lib/tmp_txt_result_file_20150508170116.txt";
$file_realpath = realpath($res_filepath);
$file_basename = basename($res_filepath);
// $file_filesize = filesize($res_filepath);
$file_fileinfo = pathinfo($res_filepath