PHP include()适当的Mime类型 [英] Php include() proper Mime-types

查看:65
本文介绍了PHP include()适当的Mime类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

TLDR:我正在寻找一种方法来从保存了mime类型的文件系统中提供Web资源(css,jpg等).

我正在创建一个php包装器,该包装器将验证来自另一个域的用户(通过一次令牌),创建一个会话,然后将文件(位于单独目录中的多个网站)提供给该用户(否则位于可访问的目录中)./p>

到目前为止,我已经使用php进行验证,并使用mod_rewrite通过包装器将对目录内任何内容的所有请求重定向,将请求的页面添加为get.

问题是,当我打开一个链接到其他资源的网页时,它会针对每个资源通过包装运行,并且不会保留mime类型.结果格式化就搞砸了.

是否有人可以通过php脚本来解决文件系统中的文件,或者有更好的方法.

这是我的重定向规则:

 <目录/var/www/ssl/auth>RewriteEngine开RewriteBase/authRewriteRule ^(.*)/wrapper.php?topage=https://%{SERVER_NAME}/auth/$1</目录> 

而且这包括:(在检查文件是否存在之后)

  $ path ='https://'.$_SERVER ['SERVER_NAME'].'/';$ abs_path = substr($ _ GET ['topage'],strlen($ path));包括($ abs_path); 

这是我遇到的错误(在chrome上使用开发工具)

 资源解释为样式表,但以MIME类型text/html传输 

我正在创建它,以便我学校的Apple用户可以发布需要身份验证的班级网站(使用iweb并在内部托管),并且从学校的主要网站(外部托管)进行链接时无需登录即可访问.

诉讼坚持认为,他们希望将其设置为跨域.如果您对此特定问题或如何实现身份验证有任何建议.让我知道.

解决方案

坦杜.感谢您使用您提出的概念所提供的帮助,我能够使用堆栈溢出来解决我的问题,这里就是解决方案.我<并找到了如何获取mime类型,然后我使用 header()进行中继的工作方式就像一个护身符.

  $ path = $ proto.$ _ SERVER ['SERVER_NAME'].'/';$ abs_path = substr($ _ GET ['topage'],strlen($ path));$ type =(get_object_vars(apache_lookup_uri("$ abs_path"))));;header('Content-type:'.$ type ['content_type']);readfile($ abs_path); 

希望这可以帮助某人.

解决方案

好吧,我不确定是否可以回答您的完整问题,但是Chrome中的问题是您的服务器发送的文件是错误的MIME类型(明显地).您需要更新服务器设置,以为css,js等文件发送正确的mime类型.很难猜测css,js等的mime类型,因为它们没有存储在文件头中的mime信息,而您必须猜测.您可以配置apache以根据具有mime.types扩展名的mime类型进行设置.

TLDR: I'm looking for a way to serve web resources (css, jpg, etc) from the filesystem that preserves mime-type.

I am creating a php wrapper that validates a user from another domain (via a one time token), creates a session and then serves files, several websites in separate directories, inside of a otherwise in accesable directory to that user.

I have, so far, used php to handle validation and mod_rewrite to redirect all requests for anything inside of the directory through the wrapper adding the requested page as a get.

The problem is, when I open a webpage that links to other resources it runs through the wrapper for each resource and doesn't preserve the mime-type. As a result formating gets screwed up.

Does anyone have a work around or a better way to serve files from the filesystem via a php script.

Here are my Redirect rules:

<Directory /var/www/ssl/auth>
    RewriteEngine On
    RewriteBase /auth
    RewriteRule ^(.*) /wrapper.php?topage=https://%{SERVER_NAME}/auth/$1
</Directory>

And This is the include: (after it's been checked that the file exists)

$path = 'https://'.$_SERVER['SERVER_NAME'].'/';
$abs_path=substr($_GET['topage'], strlen($path));
include ($abs_path);

Here are the errors I'm getting (using dev tools on chrome)

Resource interpreted as Stylesheet but transfered with MIME type text/html

I am creating this so apple users at my school can publish class websites (using iweb and hosted internally) that require authentication and can be accessed without signing in twice when linked from the school's main website (externally hosted).

The suits are adamant that they want it setup to cross domains. If you have any suggestions to this particular problem or how to achieve auth. across domains let me know.

Solution

Tandu. Thanks for the help using the concepts you laid out I was able to use stack overflow to solve my problem here is the solution. I < and found how to get the mime type and then I relayed it using header() works like a charm.

$path = $proto.$_SERVER['SERVER_NAME'].'/';
$abs_path=substr($_GET['topage'], strlen($path));
$type = (get_object_vars(apache_lookup_uri("$abs_path")));
header('Content-type: '.$type['content_type']);     
readfile($abs_path);

Hope this helps someone down the line.

解决方案

Well, I'm not sure if I can answer your full question, but the problem in Chrome is that your server is sending the file as the wrong MIME type (obviously). You need to update your server settings to send the correct mime type for css, js, etc. files. It is difficult to guess the mime type for css, js, etc. because they don't have the mime information stored in the file headers and instead you must guess. You can configure apache to set the mime type based on the extension with the mime.types.

这篇关于PHP include()适当的Mime类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆