【问题标题】:Download an HTML file [closed]下载 HTML 文件 [关闭]
【发布时间】:2025-12-27 23:20:11
【问题描述】:

我想在我的网站 (html/php) 中创建一个链接以下载 HTML 文件,而不在导航器中显示此文件。有可能吗?

【问题讨论】:

    标签: html file download


    【解决方案1】:

    编写一个小 PHP 脚本,强制浏览器下载文件:

    $fullPath = 'the/Path/To/The/File.html';
    
    header('Content-Description: File Transfer'); 
    header('Content-Type: application/octet-stream'); 
    header('Content-Length: ' . filesize($fullPath)); 
    header('Content-Disposition: attachment; filename="' . basename($fullPath) . '"');
    readfile($fullPath);
    exit();
    

    【讨论】:

    • 感谢您的回答。自然地,这完美地工作。也许我问得太快了,好像我在聊天一样,但我的麻烦与我的代码的另一个细节有关:当智者指向月亮时,白痴看着手指:p