如何在 PHP 中强制下载文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3476362/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 09:53:51  来源:igfitidea点击:

How to force a file to download in PHP

phphtmldownload

提问by OM The Eternity

I have list of images and I want a "Download" link along with every image so that user can download the image.

我有图像列表,我想要一个“下载”链接以及每个图像,以便用户可以下载图像。

so can someone guide me How to Provide Download link for any file in php?

所以有人可以指导我如何为php中的任何文件提供下载链接吗?

EDIT

编辑

I want a download panel to be displayed on clicking the download link I dont want to navigate to image to be displayed on the browser

我希望在单击下载链接时显示下载面板 我不想导航到要在浏览器上显示的图像

回答by John Parker

If you want to force a download, you can use something like the following:

如果要强制下载,可以使用以下内容:

<?php
    // Fetch the file info.
    $filePath = '/path/to/file/on/disk.jpg';

    if(file_exists($filePath)) {
        $fileName = basename($filePath);
        $fileSize = filesize($filePath);

        // Output headers.
        header("Cache-Control: private");
        header("Content-Type: application/stream");
        header("Content-Length: ".$fileSize);
        header("Content-Disposition: attachment; filename=".$fileName);

        // Output file.
        readfile ($filePath);                   
        exit();
    }
    else {
        die('The provided file path is not valid.');
    }
?>

If you simply link to this script using a normal link the file will be downloaded.

如果您只是使用普通链接链接到此脚本,则会下载该文件。

Incidentally, the code snippet above needs to be executed at the start of a page (before any headers or HTML output had occurred.) Also take care if you decide to create a function based around this for downloading arbitrary files - you'll need to ensure that you prevent directory traversal (realpathis handy), only permit downloads from within a defined area, etc. if you're accepting input from a $_GET or $_POST.

顺便说一下,上面的代码片段需要在页面开始时执行(在任何标题或 HTML 输出发生之前)。如果您决定基于此创建一个函数来下载任意文件,也要注意 - 您需要确保您防止目录遍历(realpath很方便),如果您接受来自 $_GET 或 $_POST 的输入,则只允许从定义的区域内下载等。

回答by debugger

In HTML5 downloadattribute of <a>tag can be used:

在 HTML5download<a>可以使用标签的属性:

echo '<a href="path/to/file" download>Download</a>';

This attribute is only used if the href attribute is set.

There are no restrictions on allowed values, and the browser will automatically detect the correct file extension and add it to the file (.img, .pdf, .txt, .html, etc.).

此属性仅在设置了 href 属性时使用。

对允许值没有限制,浏览器会自动检测正确的文件扩展名并将其添加到文件中(.img、.pdf、.txt、.html 等)。

Read more here.

在这里阅读更多。

回答by Buzogany Laszlo

The solution is easier that you think ;) Simple use:

解决方案比您认为的更容易;) 简单使用:

header('Content-Disposition: attachment');

And that's all. Facebook for example does the same.

就这样。例如,Facebook 也是如此。

回答by Chris Harrison

You can do this in .htaccess and specify for different file extensions. It's sometimes easier to do this than hard-coding into the application.

您可以在 .htaccess 中执行此操作并指定不同的文件扩展名。有时这样做比在应用程序中进行硬编码更容易。

<FilesMatch "\.(?i:pdf)$">
  ForceType application/octet-stream
  Header set Content-Disposition attachment
</FilesMatch>

By the way, you might need to clear browser cache before it works correctly.

顺便说一下,您可能需要清除浏览器缓存才能正常工作。