Getting a file to download instead of opening the

2019-01-15 23:17发布

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. I have tried adjusting the .htaccess file, but no luck.

How do you get a file to download, instead of opening in the browser?

3条回答
叛逆
2楼-- · 2019-01-15 23:32

you can try adding these line of code to your .htaccess file

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

it works well for me

查看更多
相关推荐>>
3楼-- · 2019-01-15 23:47

In order to force the browser to download the file, you have to set the HTTP Content-Disposition header to attachment. There is an easy way to configure your HTTP server to add this header to all .kml files and a PHP-only way.

1. Configure Apache to add the Content-Disposition header

Place the following in your Apache configuration or in a .htaccess file inside of the directory containing your .kml files:

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

You need mod_headers enabled for this to work.

2. Use a PHP script as a proxy for sending the file to the browser

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. This way, you can control the HTTP headers with the script:

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

Thereby, I assume that $path is the path to the file on your server. 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. Note that even when you prefix the string specified in the request with the directory, it could contain ../ to access files in other directories.

查看更多
Explosion°爆炸
4楼-- · 2019-01-15 23:48

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.

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. The header to encourage download is Content-Disposition:

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

In PHP, any header directive should be served BEFORE any other content, including any whitespace.

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:-

<?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.

查看更多
登录 后发表回答