简体   繁体   English

获取文件下载而不是打开浏览器

[英]Getting a file to download instead of opening the browser

I have a site built with php, there are .kml files that I would like the user to be able to download when they click on the link, but it current opens the (xml-like) file in the browser. 我有一个用php构建的网站,有一些.kml文件,我希望用户单击链接后就可以下载,但是当前它会在浏览器中打开(类似于xml的文件)。 I have tried adjusting the .htaccess file, but no luck. 我曾尝试调整.htaccess文件,但没有运气。

How do you get a file to download, instead of opening in the browser? 如何获取文件下载,而不是在浏览器中打开?

There are two different approaches depending in your scenario. 根据您的方案有两种不同的方法。

If your KML files are static and you are serving them directly without PHP then you should look at configuring the .htaccess file appropriately and ensuring that your web server is configured to observe .htaccess files. 如果您的KML文件是静态文件,并且直接在不使用PHP的情况下提供它们,那么您应该考虑适当地配置.htaccess文件,并确保将Web服务器配置为观察.htaccess文件。

If your KML files are dynamic or are being proxied by a PHP script then you need to serve the right headers to force a download. 如果您的KML文件是动态的,或者被PHP脚本代理,那么您需要提供正确的标头来强制下载。 The header to encourage download is Content-Disposition : 鼓励下载的标题为Content-Disposition

header("Content-Disposition: attachment; filename=\"$filename\"");

In PHP, any header directive should be served BEFORE any other content, including any whitespace. 在PHP中,任何header指令都应在任何其他内容(包括任何空格)之前提供。

If you find that your server isn't configured to observe .htaccess files and you aren't able to configure it appropriately then a simple proxy script like this would work:- 如果发现您的服务器未配置为观察.htaccess文件,并且您无法正确配置它,则像这样的简单代理脚本将起作用:-

<?php
header("Content-Disposition: attachment; filename=\"myData.kml\"");
include 'myData.kml';
?>

...replacing the relevant filename. ...替换相关的文件名。 You can expand this out to work for multiple KML files using variables but for security, make sure that your script checks that any requested files are valid rather than just blindly 'including' any file that is requested. 您可以使用变量将其扩展为适用于多个KML文件,但是为了安全起见,请确保脚本检查所请求的文件是否有效,而不仅仅是盲目“包含”所请求的任何文件。

In order to force the browser to download the file, you have to set the HTTP Content-Disposition header to attachment . 为了强制浏览器下载文件,您必须将HTTP Content-Disposition标头设置为attachment There is an easy way to configure your HTTP server to add this header to all .kml files and a PHP-only way. 有一种简单的方法可以配置HTTP服务器以将此标头添加到所有.kml文件中,并且是仅PHP的方法。

1. Configure Apache to add the Content-Disposition header 1.配置Apache以添加Content-Disposition标头

Place the following in your Apache configuration or in a .htaccess file inside of the directory containing your .kml files: 将以下内容放在您的Apache配置中或包含.kml文件的目录内的.htaccess文件中:

<Files "*.kml">
    Header set Content-Disposition attachment
</Files>

You need mod_headers enabled for this to work. 您需要启用mod_headers才能使其正常工作。

2. Use a PHP script as a proxy for sending the file to the browser 2.使用PHP脚本作为代理,将文件发送到浏览器

The PHP-only solution as proposed by Purpletoucan would be to not let the browser access the file directly, but use a PHP script for sending it over the line. Purpletoucan提出的纯PHP解决方案是不让浏览器直接访问文件,而是使用PHP脚本通过该行发送文件。 This way, you can control the HTTP headers with the script: 这样,您可以使用脚本控制HTTP标头:

<?php
header('Content-Disposition: attachment; filename="' . basename($path) . '"');
readfile($path);
?>

Thereby, I assume that $path is the path to the file on your server. 因此,我假设$path是服务器上文件的路径。 If you determine the path depending on a query parameter given in the URL, you should always check that an existing file is given, that the user is allowed to download it and that it is located inside the correct directory. 如果根据URL中提供的查询参数确定路径,则应始终检查是否已给出现有文件,是否允许用户下载该文件以及该文件是否位于正确的目录中。 Note that even when you prefix the string specified in the request with the directory, it could contain ../ to access files in other directories. 请注意,即使您在请求中指定的字符串加上目录前缀,该字符串也可能包含../来访问其他目录中的文件。

you can try adding these line of code to your .htaccess file 您可以尝试将这些代码行添加到您的.htaccess文件中

<FilesMatch "\.(mp3|MP3)">
ForceType application/octet-stream
Header set Content-Disposition attachment
</FilesMatch>

it works well for me 这对我来说很有效

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM