views:

174

answers:

3

I have a page with an image. I want the client to be able to upload a new image into the database and then be able to switch out the image from within the CMS. Once they switch it in the CMS, I want PHP to edit the background image in the external css file.

Is it possible to do that? I know how to have PHP switch out an image tag in the HTML markup, but I would like to be able to edit the external css file to keep my code cleaner.

+2  A: 

If you're using Apache, you can also embed PHP code in your CSS files, by adding this to your apache conf:

AddHandler application/x-httpd-php .css
Ben James
Is this what you are referring to? http://net.tutsplus.com/tutorials/php/supercharge-your-css-with-php-under-the-hood/
zeckdude
A: 

You can use the file access functions to permanently change it or, probably easier, make the CSS file itself a PHP script and put the logic in there for deciding how the CSS is to be created. Make sure to set the caching rules appropriately for the CSS file.

Sydius
A: 

Adding to Ben and Sydius's answer which generates the dynamic CSS file.

How about having a seperate PHP driven CSS that supercedes the current one you have?

You can also manipulate it to avoid persistent cache of the older image by manipulating the CSS's URI.

 <link rel="stylesheet" href="path/to/sitelevel.css">
 <link rel="stylesheet" href="path/to/background-switcher.css.php?cache-buster123">
o.k.w