简体   繁体   English

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

[英]How to serve this file with these HTTP headers?

I am using a shared hosting, so I can't edit Apache settings. 我正在使用共享主机,因此无法编辑Apache设置。 I need to allow users to download a .crx file in this way: 我需要允许用户以这种方式下载.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"

How can I do this in PHP or in others way ? 如何使用PHP或其他方式执行此操作?

Use header() function. 使用header()函数。

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

Update: 更新:

Sorry I forgot that the file is .crx :) 对不起,我忘了该文件是.crx :)

If your file is cool_app.crx , write a wrapper, say cool_app.php : 如果您的文件为cool_app.crx ,请编写一个包装,说cool_app.php

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

Then point your link to cool_app.php . 然后将您的链接指向cool_app.php

<?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)
?>

Try adding this line: 尝试添加以下行:

AddType application/x-chrome-extension crx

to your .htaccess file. 到您的.htaccess文件。

As per my comments: 根据我的评论:

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

You first set the content type using the header function. 首先使用header功能设置内容类型。

Then, if the file exists on your server, use the readfile method to send the file, or if it's generated during execution, just echo the file contents. 然后,如果文件存在于服务器上,请使用readfile方法发送文件,或者如果文件是在执行过程中生成的,则只需回显文件内容即可。

this work fine 这项工作很好

<?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;
}
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM