PHP CSS Editor Like WordPress’s CSS Editor

by Hiroshi on February 3, 2013

in File

Sometimes clients need to edit their CSS to make design changes and you have to implement a mechanism in website admin to get CSS, edit it and save it. You can do it in several ways but here is popular one like WordPress use while theme editing. Here it is.

Call CSS in Textarea like this:

<?php
// For Old Version of PHP than PHP 5
$file = file_get_contents('styles.css', true);
// For PHP 5
$file = file_get_contents('styles.css', FILE_USE_INCLUDE_PATH);
?>

Call CSS, edit that CSS and save changes like below:

<?php
$file = 'styles.css';
// Open the file to get existing content
$current = file_get_contents($file);
// Append a new person to the file
$current .= "John Smith\n";
// Write the contents back to the file
file_put_contents($file, $current);
?>

For more explanation see this and this.

Related Posts

Previous post:

Next post: