我对HTML有一个基本的概念。我想在我的样本网站创建下载链接,但我不知道如何创建它。我如何制作一个下载文件的链接而不是访问它?


当前回答

我知道我迟到了,但这是我搜索1小时后得到的

 <?php 
      $file = 'file.pdf';

    if (! file) {
        die('file not found'); //Or do something 
    } else {
       if(isset($_GET['file'])){  
        // Set headers
        header("Cache-Control: public");
        header("Content-Description: File Transfer");
        header("Content-Disposition: attachment; filename=$file");
        header("Content-Type: application/zip");
        header("Content-Transfer-Encoding: binary");
        // Read the file from disk
        readfile($file); }
    }

    ?>

对于可下载链接,我是这样做的

<a href="index.php?file=file.pdf">Download PDF</a>

其他回答

在支持HTML5的现代浏览器中,可以实现以下功能:

<a href="link/to/your/download/file" download>Download link</a>

你也可以用这个:

<a href="link/to/your/download/file" download="filename">Download link</a>

这将允许您更改实际正在下载的文件的名称。

要链接到该文件,请执行与任何其他页面链接相同的操作:

<a href="...">link text</a>

要强制下载即使它们有嵌入式插件(Windows + QuickTime = ugh),你可以在htaccess / apache2.conf中使用这个:

AddType application/octet-stream EXTENSION

像这样

<a href="www.yoursite.com/theThingYouWantToDownload">Link name</a>

因此,example.com网站上的文件名。jpg将如下所示

<a href="www.example.com/name.jpg">Image</a>

我知道我迟到了,但这是我搜索1小时后得到的

 <?php 
      $file = 'file.pdf';

    if (! file) {
        die('file not found'); //Or do something 
    } else {
       if(isset($_GET['file'])){  
        // Set headers
        header("Cache-Control: public");
        header("Content-Description: File Transfer");
        header("Content-Disposition: attachment; filename=$file");
        header("Content-Type: application/zip");
        header("Content-Transfer-Encoding: binary");
        // Read the file from disk
        readfile($file); }
    }

    ?>

对于可下载链接,我是这样做的

<a href="index.php?file=file.pdf">Download PDF</a>

下载链接是指向您想要下载的资源的链接。它的构造方式与其他链接相同:

<a href="path to resource.name of file">Link</a>

<a href="files/installer.exe">Link to installer</a>