I need to check whether a css file is available or - if not - load a local fallback file. What's the fastest way in php?
-
Cache the remote file, only serve this one to the user and update it only from time to time. – KingCrunch Nov 15 '11 at 15:46
-
If I cache it and the file is later not available, everything will break, right? – kaiser Nov 15 '11 at 15:47
-
2@kaiser If you cache it, and the file is later not available, then your users will still be served the cached file. – Nov 15 '11 at 16:57
5 Answers
There is no fastest way. If you need and want to probe a remote file, then that requires a HTTP request. There are multiple options for that, but only with slight performance differences. The actual delay comes from issuing a HTTP transfer.
get_headers()
is the simplest option:
$exists = strstr(current(get_headers($url)), "200");

- 144,265
- 20
- 237
- 291
-
From micro time measuring, it works 50% faster than `file_get_contents();`. Thanks! – kaiser Nov 15 '11 at 16:11
-
1This does work faster, but I also found that it does not work with certain server configs. Whereas `file_get_contents` works regardless. – John Contarino Dec 10 '15 at 21:18
I recommend you first to load your CSS fallback file in the HTML, and than you load the other CSS file. If the second one isn't available, the first one will give your format. Otherwise it will be overwritten, by the second file.
Example:
<link rel="stylesheet" type="text/css" href="fallback.css" />
<link rel="stylesheet" type="text/css" href="yourstyle.css" />
You can also do it on the server side with PHP, if you really wan't to do this.
Example:
<?php
if (file_exists('filepath.css'))
echo '<link rel="stylesheet" type="text/css" href="yourstyle.css" />';
else
echo '<link rel="stylesheet" type="text/css" href="fallback.css" />';
?>
This would do it server side, if you really wan't it like this.

- 5,288
- 2
- 26
- 41
-
1this will still load the fallback anyways, so if the stylesheets are big this might create more traffic. Maybe another way could be using javascript to check a certain property which the css should have set, and if it is not: load the other one. – Flo Nov 15 '11 at 15:53
-
Yes, but if you're looking for traffic and you think you have many visitors, you shouldn't check every time when a page is loaded if a file exists, because this is a bit overhead. If you really want to improve it you should use something like **Minify**. – evotopid Nov 15 '11 at 16:04
-
jea checking on serverside is overhead too, hence the idea with the javascript checking if a certain style which is defined in the css is set. If not you can assume that it did not load and then load the other one. (if that is possible in js, not 100% sure right now) – Flo Nov 15 '11 at 16:07
I'd go for leo's answer, but if you really want to do it your way, use file_get_contents()
The function returns the read data or FALSE on failure.

- 1,678
- 10
- 14
I'm guessing this file is remote and if not to then fall back on the local? Otherwise as the first answer suggests just load both and fall back appropriately, there's an insignificant overhead to this in most cases. But seeing as you mention falling back to local assume you are trying to load a remote file in which case use the fopen
in read only mode and then echo or return a variable with the appropriate file path, such as...
$filename = 'http://uk.php.net/css/style.css'; // Check to see if the file exists by trying to open it for read only if (fopen($filename, 'r')) { $css = 'href="http://uk.php.net/css/style.css"'; } else { $css = 'href="style.css"'; }
But to be honest it's a little excessive unless your site has considerable traffic and even then I'm not sure which would have the greater overhead.
you can use is_file()
function in php to check if that file is available.
http://php.net/is_file

- 379
- 2
- 11