php文件被上传到www文件夹的一侧如何制作可下载的链接

时间:2015-01-13 07:32:04

标签: php html

应用程序服务器目录 -

/var/www/html/testweb/index.php
c:/wamp/www/testweb/index.php

文件已上传至

/var/documents/file1.pdf
c://documents/file1.pdf

现在该文档不在www根目录中,我可以链接它们,例如,如果文档位于:

/var/www/hmtl/testweb/document1/file1.pdf
c://wamp/www/testweb/document1/file1.pdf

很容易添加像

这样的href链接
<a href="document1/file1.pdf>download</a>

如果链接超出www根目录

,如何添加下载链接

问候

4 个答案:

答案 0 :(得分:2)

假设安全模式已关闭并且移动它不是一个选项,您可以使用file_get_contents()检索文件数据,.htaccess将网址更改为.pdf,然后header()更改为为文件设置正确的标头。基本上,创建一个新的PHP脚本,返回带有正确标题的文件,然后使用.htaccess使浏览器认为它是一个合法的PDF文件。

答案 1 :(得分:2)

首先让你的文件可以读取php runner(例如ubuntu中的apache的www-data)。然后链接到一个php文件:

<a href='http://yourhost/path/to/downloader.php'>Download file</a>

并修改downloader.php以使您的文件可下载:

    $filename = 'path/to/your/file';
    $content = file_get_contents($filename);
    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename='.basename($filename));
    header('Expires: 0');
    header('Cache-Control: must-revalidate');
    header('Pragma: public');
    header('Content-Length: ' . strlen($content));
    die($content);

$filename='path/to/your/file'替换为$filename='c://documents/file1.pdf'

答案 2 :(得分:1)

非常简单,你可以在httpd.conf中添加更多目录,也可以设置虚拟主机。

添加目录

左键单击wamp&gt; apache&gt; htttp.conf

在最后添加 别名/ WEBSITENAME“X:/ PATH / TO / FILES /”

<Directory "X:/PATH/TO/FILES/">
    Options Indexes FollowSymLinks MultiViews
    AllowOverride all
        Order Allow,Deny
             Allow from all
</Directory>
  
    

http://forum.wampserver.com/read.php?2,72262,76713

  

仅为localhost设置访问权限

Order Deny,Allow
Deny from all
Allow from 127.0.0.1

答案 3 :(得分:0)

如何指定完整路径。

<a href="file://c://wamp/www/testweb/document1/file1.pdf>download</a>

希望这有助于你