How do I edit an external css file with PHP?

谁都会走 提交于 2019-12-08 11:58:02

问题


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


来源:https://stackoverflow.com/questions/1584388/how-do-i-edit-an-external-css-file-with-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!