显示下载页面后使用php下载文件

显示下载页面后使用php下载文件,php,mysql,Php,Mysql,我有一个文件要通过PHP下载(只允许登录用户才能下载)。下载页面有相同的评级和评论系统,但文件下载会立即开始,而不显示下载页面。我如何解决这个问题 <?php require("usercheck.php"); $file = 'D:\dd.pdf'; if (file_exists($file)) { header('Content-Description: File Transfer'); header('Content-Type: application/

我有一个文件要通过PHP下载(只允许登录用户才能下载)。下载页面有相同的评级和评论系统,但文件下载会立即开始,而不显示下载页面。我如何解决这个问题

<?php

require("usercheck.php");
$file = 'D:\dd.pdf';


if (file_exists($file)) 

{

    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename='.basename($file));
    header('Content-Transfer-Encoding: binary');
    header('Expires: 0');
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    header('Pragma: public');
    header('Content-Length: ' . filesize($file));
    ob_clean();
    flush();
    readfile($file);
    exit;
}


?>

<!DOCTYPE HTML>

<html lang="en-US">
<head>
    <meta charset='UTF-8'>
    <meta http-equiv="X-UA-compatible" content="IE-edge">
    <meta name="viewport" content="width-device-width">

    <title>Download</title>

    <?php require("headfiles.php");?>
    <link href="star/star-rating.css" media="all" rel="stylesheet" type="text/css"/>
    <script src="star/star-rating.js" type="text/javascript"></script>
</head>
<body>

----------------------Body contents----------------

下载
----------------------正文内容----------------

现在无法使用,因为您正在下载页面顶部加载文件。 我会将html页面标题上方的PHP移动到一个单独的文件中,并从下载页面链接到它。如果希望立即从下载页面开始下载,可以使用JavaScript强制下载。不要忘记在下载页面和文件上使用身份验证检查