I found this on a website,
css_loader.php
hey have you checked if your server appends a few lines to your code (for example an analytics code) . if so you must add a rule in .htaccess that stops the server for adding lines to your code. this can be done by adding this to .htaccess - "php_value auto_append_file none" without quotes of course . i hope this helps
I wrote this class for such needs, it can render the output and compress it, later i can add write a file of the end results, link here
Stoney has shown you your error .... now for a more improved version
header('Content-type: text/css');
ob_start("compress");
function compress($buffer) {
/* remove comments */
$buffer = preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $buffer);
/* remove tabs, spaces, newlines, etc. */
$buffer = str_replace(array("\r\n", "\r", "\n", "\t", ' ', ' ', ' '), '', $buffer);
return $buffer;
}
/* your css files */
include('main.css');
include('menu.css');
include('content.css');
ob_end_flush();
You have an mistake in your code, here's the correct code:
<?php
// First of all send css header
header("Content-type: text/css");
// Array of css files
$css = array(
'main.css',
'menu.css',
'content.css'
);
// Prevent a notice
$css_content = '';
// Loop the css Array
foreach ($css as $css_file) {
// Load the content of the css file
$css_content .= file_get_contents($css_file);
}
// print the css content
echo $css_content;
?>
And I hope the files are in the same folder. Perhaps you should use __DIR__
or dirname(__FILE__)
to get the relative path to your files.
<?php
// Array of css files
$css = array(
'first.css',
'second.css'
);
$mergeCSS = "";
// Loop the css Array
foreach ($cssas $css_file) {
// Load the content of the css file
$mergeCSS.= file_get_contents($css_file);
}
// Remove comments also applicable in javascript
$mergeCSS= preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $mergeCSS);
// Remove space after colons
$mergeCSS= str_replace(': ', ':', $mergeCSS);
// Remove whitespace
$mergeCSS= str_replace(array("\n", "\t", ' ', ' ', ' '), '', $mergeCSS);
//Generate Etag
$genEtag = md5_file($_SERVER['SCRIPT_FILENAME']);
// call the browser that support gzip, deflate or none at all, if the browser doest support compression this function will automatically return to FALSE
ob_start('ob_gzhandler');
// call the generated etag
header("Etag: ".$genEtag);
// Same as the cache-control and this is optional
header("Pragma: public");
// Enable caching
header("Cache-Control: public ");
// Expire in one day
header('Expires: ' . gmdate('D, d M Y H:i:s', time() + 86400 ) . ' GMT');
// Set the correct MIME type, because Apache won't set it for us
header("Content-type: text/javascript");
// Set accept-encoding
header('Vary: Accept-Encoding');
// Write everything out
echo($mergeCSS);
?>
This code also compatible for merging javascript