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.