如何使用这些HTTP标头提供此文件?

时间:2012-05-24 13:18:29

标签: php

我正在使用共享主机,因此无法编辑Apache设置。 我需要允许用户以这种方式下载.crx文件:

1) The file has the content type application/x-chrome-extension
2) The file is not served with the HTTP header X-Content-Type-Options: nosniff
3) The file is served with one of the following content types:
- empty string
- "text/plain"
- "application/octet-stream"
- "unknown/unknown"
- "application/unknown"

我怎么能用PHP或其他方式做到这一点?

5 个答案:

答案 0 :(得分:1)

使用header()功能。

header('Content-Type: text/plain');

<强>更新

抱歉,我忘记了该文件是.crx:)

如果您的文件是cool_app.crx,请写一个包装器,比如cool_app.php

<?php
header('Content-Type: application/x-chrome-extension');
include 'cool_app.crx';

然后将您的链接指向cool_app.php

答案 1 :(得分:1)

<?php
header("Content-Type: application/x-chrome-extension; charset=UTF-8");   // use here both proper settings for your case
//  header("Content-Length: " .filesize($your_crx_file));    // this is realy optional, I do recomend to not include this sentence
header('Content-Disposition: attachment; filename="TheNameThatYouWant.crx"');    // this set the name of the downloaded file on browser side.
readfile($your_crx_file)
?>

答案 2 :(得分:1)

尝试添加此行:

AddType application/x-chrome-extension crx

到您的.htaccess文件。

答案 3 :(得分:0)

根据我的评论:

header('content-type: text/plain'); // set the content-type
readfile('path-to-crx.crx');

您首先使用header功能设置内容类型。

然后,如果您的服务器上存在该文件,请使用readfile方法发送文件,或者如果在执行期间生成该文件,则只回显文件内容。

答案 4 :(得分:0)

这项工作很好

<?php
$file = 'extension.crx';

if (file_exists($file)) {
    header('Content-Description: File Transfer');
    header('Content-Type: application/x-chrome-extension');
    header('Content-Disposition: attachment; filename='.basename($file));
    header('Content-Transfer-Encoding: binary');
    header('Expires: 0');
    header('Cache-Control: must-revalidate');
    header('Pragma: public');
    header('Content-Length: ' . filesize($file));
    ob_clean();
    flush();
    readfile($file);
    exit;
}
?>