你能通过Apache下载一个文件吗?

你能通过Apache下载一个文件吗?,apache,Apache,Path=/var/lib/foo.txt 是否可以配置Apache,以便有一些HTTP URL启动此文件的下载,以及如何配置? 无.htaccess文件。 那么这个URL是什么呢localhost/var/lib/foo.txt?将此添加到.htaccess文件中。。。您可以为不同的文件类型(如.txt、.pdf、.mp4等)添加不同的指令 <Files *.txt> ForceType application/octet-stream Header set Content

Path=
/var/lib/foo.txt

是否可以配置Apache,以便有一些HTTP URL启动此文件的下载,以及如何配置?
.htaccess
文件。


那么这个URL是什么呢
localhost/var/lib/foo.txt

将此添加到.htaccess文件中。。。您可以为不同的文件类型(如.txt、.pdf、.mp4等)添加不同的指令

<Files *.txt>
  ForceType application/octet-stream
  Header set Content-Disposition attachment
</Files>

ForceType应用程序/八位组流
标题集内容处置附件

是在/var/lib/(/var/lib/.htaccess)中创建一个.htaccess文件,并添加以下内容:

AddType application/octect-stream .txt
就这些

编辑:

如果没有.htaccess文件,您可以使用PHP代码

以下是一个例子: 在
public\u html
www
文件夹中创建一个文件,并将其命名为
download.php
粘贴下面的PHP代码并保存文件。现在请访问。 该文件应无任何问题下载

$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/octect-stream");
    header("Content-Transfer-Encoding: binary");
    readfile($filePath);
}

这就像在apache配置中添加别名指令一样简单

Alias '/bar' '/path/to/foo'

然后您可以通过
http://example.com/bar

在您的php解决方案中,如果(!$filePath)有条件的
if(!$filePath)
替换为
如果(!is_readable($filePath))
,后者测试文件是否存在且可读。