How can I return static files in PHP?
Asked Answered
O

2

6

I use a .htaccess file to redirect all requests to the same index.php. From there, I decide what to do depending on URL of the incoming request.

For now, I map the request URL to directory structure relative to a source folder, so that example.com/user/create maps to source/user/create/index.php. The file located there can handle the request as needed, and generate HTML output.

But that doesn't work for static assets, the browser may request. So my idea is to find out whether a request URL ends with a file extension and map that to a directory structure relative to a assets folder, so that example.com/css/default.css would map to assets/css/default.css. But I don't know how to respond with a static file instead of HTML code from PHP.

Normally, this might be done using an additional .htaccess rule, but I'd like to change the assets folder dynamically in code.

How can I send a static file, given on the server's hard drive, to the browser client in PHP?

Overcrop answered 3/11, 2013 at 13:11 Comment(3)
You can render assets (e.g. CSS files) using PHP, but it's much slower than serving files statically. It's usually better to rebuild your CSS if/when it changes.Thingumajig
It's still quite unclear, but i'd suggest to look into the routing component of symfony2: with that you can "attach" some request urls to some controllers who handle the response,therefore serving the content you need ( symfony.com/doc/current/components/routing/introduction.html )Echolocation
I updated my question and hope my intention is clearer now.Overcrop
A
2

On the the htacess

#Expections
RewriteCond %{REQUEST_URI} ^/assets.*$
RewriteRule ^(.*)$ - [L,NC]

#You redirect to index
...

put a / at the beginning of the file path

/assets/path/to/file
Alburnum answered 3/11, 2013 at 15:37 Comment(10)
Is there a way to dynamically change the assets directory in PHP? Anyway, I will use your solution.Overcrop
I think that only if you change .htaccessAlburnum
Alright, I'll go with the .htaccess. I have one question about your example. Wouldn't it match any urls with dots inside? I don't want to match urls like example.com/path.with.dots/page. Thanks a lot for your answer!Overcrop
if you put at the .htaccess the rules rewritecond of folder with dots you need escape the dots( put \ before dot)Alburnum
I am not sure if I understood you right. I cannot make a rule for every url with dots. The distinction is that those non asset urls have slashes after the dots. In other words, the last url segment does only contain dots in asset urls.Overcrop
sorry for my english I am not good. Then i spoken that you need put a slash before dots in case if you use folder with dots. if you don't escape the dot "." at regex it is equal a any char. Example: ^/same.test.*$ if you create folder same.test or sameWtest you can access. but if the regex is equal: ^/same\.test.*$ only the folder same.test can be accessAlburnum
So I would need something like .+\.[^/]+ to find asset requests. I think that would match urls that start with anything, include a dot, and then end with anything, but no more slashes. How would the according .htaccess look like then?Overcrop
I think that you don't need use this regex .+\.[^/] because all i can access all files from folder assets. you can put some folder that contains other this folders with dots. RewriteCond %{REQUEST_URI} ^/examples.*$\ all folder or files in examples can be access..Alburnum
Alright. One last question about this. Is it possible to add the /assets/ folder as a prefix in .htaccess so that it doesn't need to be stated in the url?Overcrop
let us continue this discussion in chatAlburnum
B
3

Well, if you really want to, you can just use readfile(). But it would be better to let the web server handle static files if you can; it's more efficient than firing up PHP just to throw a file out the door.

Also, with the PHP approach you'll probably have to set the Content-Type header appropriate per file, which can be annoying. Your web server will be doing that for you already when serving files directly.

Plus there are other things that you may be getting for "free" at the moment that you'll have to consider -- using ob_gzhandler if you want to compress your pages, as might already be being done for static files by Apache, say.

So, while it's possible, and I can see reasons why it's sometimes desirable, I'd probably try to make this kind of processing the exception rather than the rule for files that aren't generally dynamic...

Bradberry answered 3/11, 2013 at 14:2 Comment(1)
Is there a library that does this ? Or would I have to make my own ?Gerry
A
2

On the the htacess

#Expections
RewriteCond %{REQUEST_URI} ^/assets.*$
RewriteRule ^(.*)$ - [L,NC]

#You redirect to index
...

put a / at the beginning of the file path

/assets/path/to/file
Alburnum answered 3/11, 2013 at 15:37 Comment(10)
Is there a way to dynamically change the assets directory in PHP? Anyway, I will use your solution.Overcrop
I think that only if you change .htaccessAlburnum
Alright, I'll go with the .htaccess. I have one question about your example. Wouldn't it match any urls with dots inside? I don't want to match urls like example.com/path.with.dots/page. Thanks a lot for your answer!Overcrop
if you put at the .htaccess the rules rewritecond of folder with dots you need escape the dots( put \ before dot)Alburnum
I am not sure if I understood you right. I cannot make a rule for every url with dots. The distinction is that those non asset urls have slashes after the dots. In other words, the last url segment does only contain dots in asset urls.Overcrop
sorry for my english I am not good. Then i spoken that you need put a slash before dots in case if you use folder with dots. if you don't escape the dot "." at regex it is equal a any char. Example: ^/same.test.*$ if you create folder same.test or sameWtest you can access. but if the regex is equal: ^/same\.test.*$ only the folder same.test can be accessAlburnum
So I would need something like .+\.[^/]+ to find asset requests. I think that would match urls that start with anything, include a dot, and then end with anything, but no more slashes. How would the according .htaccess look like then?Overcrop
I think that you don't need use this regex .+\.[^/] because all i can access all files from folder assets. you can put some folder that contains other this folders with dots. RewriteCond %{REQUEST_URI} ^/examples.*$\ all folder or files in examples can be access..Alburnum
Alright. One last question about this. Is it possible to add the /assets/ folder as a prefix in .htaccess so that it doesn't need to be stated in the url?Overcrop
let us continue this discussion in chatAlburnum

© 2022 - 2024 — McMap. All rights reserved.