php通过加http响应头强制用户下载

前端之家收集整理的这篇文章主要介绍了php通过加http响应头强制用户下载前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

下面是编程之家 jb51.cc 通过网络收集整理的代码片段。

编程之家小编现在分享给大家,也给大家做个参考。

<?PHP
$filename = $_GET['file']; //Get the fileid from the URL
// Query the file ID
$query = sprintf("SELECT * FROM tableName WHERE id = '%s'",MysqL_real_escape_string($filename));
$sql = MysqL_query($query);
if(MysqL_num_rows($sql) > 0){
    $row = MysqL_fetch_array($sql);
    // Set some headers
    header("Pragma: public");
    header("Expires: 0");
    header("Cache-Control: must-revalidate,post-check=0,pre-check=0");
    header("Content-Type: application/force-download");
    header("Content-Type: application/octet-stream");
    header("Content-Type: application/download");
    header("Content-Disposition: attachment; filename=".basename($row['FileName']).";");
    header("Content-Transfer-Encoding: binary");
    header("Content-Length: ".filesize($row['FileName']));

    @readfile($row['FileName']);
    exit(0);
}else{
    header("Location: /");
    exit;
}
?>

<a href="downloadFile.PHP?file=7383">Download</a>

以上是编程之家(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。

如果觉得编程之家网站内容还不错,欢迎将编程之家网站推荐给程序员好友。

猜你在找的PHP相关文章