php输出(下载)文件

经常可以看到点击一个文件,然后就可以下载。这个功能在php中实现可以通过下面的代码:

if(is_file($file))
{
    header("Content-Type: application/force-download");
    header("Content-Disposition: attachment; filename=".basename($file));
    readfile($file);
}else {
    echo "文件不存在!";
}

 注意:

  如果所在的页面中也包含html的标签(比如form),那么这些标签要放在上面的下载脚本的下面才可以正常,否则所输出的文件会导致乱码。

原文地址:https://www.cnblogs.com/SevenwindMa/p/3997451.html