Php 如何使用这些HTTP头提供此文件?
我正在使用共享主机,因此无法编辑Apache设置。 我需要允许用户以以下方式下载.crx文件:Php 如何使用这些HTTP头提供此文件?,php,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 typ
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或其他方式实现这一点?使用函数
header('Content-Type: text/plain');
更新:
对不起,我忘了文件是。crx:)
如果您的文件是cool\u app.crx
,请编写一个包装,比如cool\u app.php
:
<?php
header('Content-Type: application/x-chrome-extension');
include 'cool_app.crx';
使用函数
header('Content-Type: text/plain');
更新:
对不起,我忘了文件是。crx:)
如果您的文件是cool\u app.crx
,请编写一个包装,比如cool\u app.php
:
<?php
header('Content-Type: application/x-chrome-extension');
include 'cool_app.crx';
根据我的评论:
header('content-type: text/plain'); // set the content-type
readfile('path-to-crx.crx');
首先使用标题
功能设置内容类型
header('Content-Type: text/plain');
然后,如果该文件存在于您的服务器上,请使用readfile
方法发送该文件,或者如果该文件是在执行过程中生成的,只需回显文件内容即可。根据我的评论:
header('content-type: text/plain'); // set the content-type
readfile('path-to-crx.crx');
<?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)
?>
首先使用标题
功能设置内容类型
header('Content-Type: text/plain');
然后,如果服务器上存在该文件,请使用readfile
方法发送该文件,或者如果该文件是在执行过程中生成的,则只需回显文件内容即可。尝试添加此行:
<?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)
?>
AddType application/x-chrome-extension crx
添加到.htaccess文件。尝试添加此行:
AddType application/x-chrome-extension crx
到您的.htaccess文件。这很好
<?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;
}
?>
这项工作很好
<?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;
}
?>
当用户下载文件时,服务器如何发送这些头?@xRobot您需要通过PHP脚本代理下载。因此,您给用户的不是文件的绝对URL,而是http://www.mysite.com/download.php?file=file.crx
。另外请注意,您的声明我使用的是共享主机,因此我无法编辑Apache设置可能不正确,您可能可以使用.htaccess文件。当用户下载文件时,服务器如何发送这些头?@xRobot您需要通过PHP脚本代理下载。因此,您给用户的不是文件的绝对URL,而是http://www.mysite.com/download.php?file=file.crx
。另外请注意,您的语句我使用的是共享主机,因此我无法编辑Apache设置
可能不正确,您可能可以使用.htaccess文件。使用getallheaders
可以获得所有当前请求头的列表,这意味着您可以找到并匹配内容类型
和x-content-type-options
以确保它是有效的传入请求,并且正如@bsdnoobz所说的,您可以在返回响应时使用头
来设置内容类型。但是我可以在PHP文件中插入头函数,而不是在.crx文件中。否?使用getallheaders
可以获得所有当前请求头的列表,这意味着您可以找到并匹配content-type
和x-content-type-options
,以确保它是有效的传入请求,正如@bsdnoobz所说,然后,您可以使用header
在返回响应时设置内容类型。但是我可以在PHP文件中插入header函数,而不是在.crx文件中。不