5

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?

user3761308
  • 744
  • 3
  • 14
  • 30
  • Possible duplicate of [Apache directive for file downloads](https://stackoverflow.com/questions/8056839/apache-directive-for-file-downloads) – Misa Lazovic Aug 18 '17 at 14:14

3 Answers3

4

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);
}
HTeuMeuLeu
  • 1,851
  • 1
  • 7
  • 17
Bismarck
  • 115
  • 1
  • 8
  • 1
    In your php solution, you'd be better to swap your conditional `if(!$filePath)` for `if(!is_readable($filePath))`, the latter tests to see if the file exists and is readable. – Progrock Aug 21 '17 at 07:37
2

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.

Progrock
  • 7,373
  • 1
  • 19
  • 25
0

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>
Chris Hawkes
  • 11,923
  • 6
  • 58
  • 68