Path = /var/lib/foo.txt
Is it possible to configure Apache so there would be some HTTP URL that would initiate a download of this file and how?
Without .htaccess
file.
And what would that URL be then? localhost/var/lib/foo.txt
?
Path = /var/lib/foo.txt
Is it possible to configure Apache so there would be some HTTP URL that would initiate a download of this file and how?
Without .htaccess
file.
And what would that URL be then? localhost/var/lib/foo.txt
?
Yes create an .htaccess file in /var/lib/ (/var/lib/.htaccess) and add the following content:
AddType application/octet-stream .txt
That's all.
EDIT:
Without an .htaccess file you can use PHP code.
Here is an example:
Create a file in public_html
or www
folder and name it download.php
Paste the PHP code below and save your file. Now visit http://www.yoursite.com/download.php.
The file should download without any issue.
$fileName = 'my-foo-file.txt';
$filePath = '/var/lib/foo.txt';
if(!$filePath){
die('File not found!');
} else {
header("Cache-Control: public");
header("Content-Disposition: attachment; filename=$fileName");
header("Content-Type: application/octet-stream");
header("Content-Transfer-Encoding: binary");
readfile($filePath);
}
It's as simple as adding an alias directive within your apache config.
Alias '/bar' '/path/to/foo'
Then you'd access the resource publicly via something like http://example.com/bar
.
Add this to your .htaccess file... You can add different directives for different file types such as .txt, .pdf, .mp4 etc...
<Files *.txt>
ForceType application/octet-stream
Header set Content-Disposition attachment
</Files>