如何创建PHP"下载即将开始"页面?

Roh*_*han 5 php

我想创建一个显示消息的PHP页面

Your download will begin shortly.

If it does not start, please click here to restart the download
Run Code Online (Sandbox Code Playgroud)

即,主要网站上存在的相同类型的页面.

它会像这样工作:

<a href="download.php?file=abc.zip">Click here</a>
Run Code Online (Sandbox Code Playgroud)

当用户点击该链接时,他将被引导到download.php,向他显示该消息,然后提供该文件以供下载.

我怎样才能做到这一点?

非常感谢!

Bor*_*lid 2

该链接需要执行以下两件事之一:

  • 直接指向网络服务器上的文件
  • 指向一个 PHP 脚本,该脚本除了设置适当的标头并将文件用作页面正文之外什么也不做。无文本输出!请参阅http://teddy.fr/blog/how-serve-big-files-through-php了解如何实际提供文件。

让浏览器“自行”开始下载的一种方法是使用 META REFRESH 标记。

另一种方法是使用 JavaScript,例如(来自 Mozilla 的 Firefox 下载页面):

function downloadURL() {
    // Only start the download if we're not in IE.
    if (download_url.length != 0 && navigator.appVersion.indexOf('MSIE') == -1) {
        // 5. automatically start the download of the file at the constructed download.mozilla.org URL
        window.location = download_url;
    }
}

// If we're in Safari, call via setTimeout() otherwise use onload.
if ( navigator.appVersion.indexOf('Safari') != -1 ) {
    window.setTimeout(downloadURL, 2500);
} else {
    window.onload = downloadURL;
}
Run Code Online (Sandbox Code Playgroud)