How do I edit an external css file with PHP?

2019-06-09 03:18发布

问题:

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.

回答1:

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


回答2:

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.



回答3:

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">