- /** CSS Update class, update the version of the image within the CSS file
- * date:2013-02-05
- * Author:fdipzone
- * ver:1.1
- * edit:bbs.it-home.org
- * Func:
- * Update ();
- *
- * ver:1.1 Add search_child parameter, can traverse sub-folder
- */
- Class cssupdate{
- Private $csstmpl _path = null;
- Private $CSS _path = null;
- Private $replacetags = Array ();
- Private $search _child = false;
- Private $convert _num = 0;
- Private $is _ready = 0;
- /** initialization
- * @param String $csstmpl _path css template path
- * @param String $css _path CSS Target path
- * @param Array $replacetags The type of picture you want to replace
- * @param boolean $search _child whether to traverse subfolders, false by default
- */
- Public function __construct ($csstmpl _path, $css _path, $replacetags =array (), $search _child=false) {
- if (!is_dir ($csstmpl _path) | | |!is_dir ($css _path) | |! $replacetags) {
- $this->is_ready = 0;
- }else{
- $this->csstmpl_path = $csstmpl _path;
- $this->css_path = $css _path;
- $this->replacetags = $replacetags;
- $this->search_child = $search _child;
- $this->is_ready = 1;
- }
- }
- /** Updating CSS files */
- Public Function Update () {
- if ($this->is_ready==0) {
- $this->response (' Csstmpl or Csspath or replacetags error ');
- Return ';
- }
- $this->traversing ($this->csstmpl_path);
- $this->response (' Covert num: '. $this->convert_num);
- }
- /** traversing a folder
- * @param String $path file path
- */
- Private function traversing ($path) {
- $handle = Opendir ($path);
- while (($file =readdir ($handle))!==false) {
- if ($file! = ': ' && $file! = '. ') {
- $curfile = $path. ' /'. $file;
- if (Is_dir ($curfile)) {//Folder
- if ($this->search_child) {//need to traverse subfolders
- $this->traversing ($curfile);
- }
- }elseif ($this->checkext ($curfile)) {//CSS file
- $dfile = Str_replace ($this->csstmpl_path, $this->css_path, $curfile);
- $this->create ($curfile, $dfile);
- $this->response ($curfile. ' Convert to '. $dfile. ' Success ');
- $this->convert_num + +;
- }
- }
- }
- Closedir ($handle);
- }
- /** Check file suffix */
- Private Function Checkext ($file) {
- $name = basename ($file);
- $namefrag = Explode ('. ', $name);
- if (count ($namefrag) >=2) {
- if (Strtolower ($namefrag [Count ($namefrag)-1] = = = ' css ') {//CSS file
- return true;
- }
- }
- return false;
- }
- /** Replace template content, write Csspath
- * @param String $tmplfile template file
- * @param String $dfile target file
- */
- Private function Create ($tmplfile, $dfile) {
- $css _content = file_get_contents ($tmplfile);
- foreach ($this->replacetags as $tag) {
- $css _content = Str_replace ($tag, $tag. "?". Date (' Ymdhis '), $css _content);
- }
- if (!is_dir (DirName ($dfile))) {//Build target path
- mkdir (DirName ($dfile), 0755, true);
- }
- File_put_contents ($dfile, $css _content, True);
- }
- /** Output */
- Private function Response ($content) {
- echo $content. "
";
- }
- }
- ?>
Copy Code2, demo example demo.php
- Require_once "CSSUpdate.class.php";
- Define (' Root_path ', DirName (__file__));
- $css _path = Root_path. ' /css ';
- $csstmpl _path = Root_path. ' /csstmpl ';
- $replacetags = Array ('. png ', '. jpg ', '. gif ');
- $cssobj = new Cssupdate ($csstmpl _path, $css _path, $replacetags);
- $cssobj->update ();
- ?>
Copy CodeAttached, PHP CSS Update class source download address. |