Php include() proper Mime-types

╄→尐↘猪︶ㄣ 提交于 2021-02-10 15:56:43

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/6067132/php-include-proper-mime-types

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!