【问题标题】:Mime-type of downloading file下载文件的 MIME 类型
【发布时间】:2010-11-07 16:13:08
【问题描述】:

我正在尝试创建可下载的视频文件。在我的网站上有一个文件列表。 所有视频均为 .flv 格式(闪存)。所有视频都有指向该文件的确切链接。 但是在所有浏览器中单击内容后都会加载到浏览器的窗口中。我不需要这个。据我了解,我应该创建包含下载文件的 mime 类型的重定向页面。我究竟应该怎么做? 语言:php

【问题讨论】:

    标签: php download mime


    【解决方案1】:

    使用以下内容创建一个 PHP 页面:

    <?php
    
    $filepath = "path/to/file.ext";
    
    header("Cache-Control: public");
    header("Content-Description: File Transfer");
    header("Content-Disposition: attachment; filename=$filepath");
    header("Content-Type: mime/type");
    header("Content-Transfer-Encoding: binary");
    // UPDATE: Add the below line to show file size during download.
    header('Content-Length: ' . filesize($filepath));
    
    readfile($filepath);
    
    ?>
    

    $filepath设置为要下载的文件的路径,将Content-Type设置为要下载的文件的mime类型。

    将“下载”链接指向此页面。

    对于同一类型的多个文件:

    <?php
    
    $filepath = $_GET['filepath'];
    
    header("Cache-Control: public");
    header("Content-Description: File Transfer");
    header("Content-Disposition: attachment; filename=$filepath");
    header("Content-Type: mime/type");
    header("Content-Transfer-Encoding: binary");
    // UPDATE: Add the below line to show file size during download.
    header('Content-Length: ' . filesize($filepath));
    
    readfile($filepath);
    
    ?>
    

    替换上面指定的信息,并使用包含文件路径的名为“filepath”的 GET 参数将“下载”链接指向此页面。

    例如,如果您将此 php 文件命名为“download.php”,则将名为“movie.mov”的文件(与 download.php 位于同一目录中)的下载链接指向“download.php?filepath=movie” .mov”。

    【讨论】:

    • 据我所知 download.php?filepath=movie.mov 不好 =) 因为每个人都可以从服务器下载任何文件。不过还是谢谢。
    • 或者,您可以为每个电影下载文件创建一个 php 文件。但这可能会变得乏味。
    • 对不起,但你的方法不起作用=(。在转到脚本后,我从浏览器得到一个窗口来下载。这就是我需要的,但它下载的文件是 0 字节 =(
    • 那是因为没有指定文件大小。您可以通过添加另一个标题来做到这一点: header('Content-Length: ' .filesize($filepath));
    • 抱歉,这是我的问题 ;) 感谢您的帮助。
    【解决方案2】:

    为此推荐的 MIME 类型是 application/octet-stream

    “八位字节流”子类型用于指示主体包含任意二进制数据。 […]

    对于接收“application/octet-stream”实体的实现,推荐的操作是简单地将数据放入文件中,取消任何 Content-Transfer-Encoding,或者将其用作用户指定的进程。

    【讨论】:

      猜你喜欢
      • 2011-11-09
      • 2016-12-23
      • 1970-01-01
      • 1970-01-01
      • 2017-05-20
      • 2017-06-01
      • 1970-01-01
      • 2021-04-13
      • 1970-01-01
      相关资源
      最近更新 更多