| 加蓝php生成静态文件的类 |
| 责任编辑: Jolr 更新日期: 2008-02-14 |
相信现在很多网部都使用PHP生成静态html文件加优化网站和并高网站的性能了,下面我将加蓝网文章管理系统生成html的php文件分享一下,本文可以帮助大家在PHP安全模式下也能建正常使用,希望能大家有所帮助 <?php
include_once(dirname(__FILE__).DIRECTORY_SEPARATOR.'WebFTP.inc.php');
class toHtml extends WebFTP {
var $tpl_path = './'; var $data_path = './'; var $suffix = '.shtml'; var $place = ''; var $places = ''; var $nonce_dir = ''; var $arr = array(); var $ftp_html_path = ''; var $www_root = '';
/** * make html file * @param $tplname create the html file template * @param $htmlname The file name we will create. */ function makeHtml($content,$DOCUMENT_ROOT,$filename) { // 建立 html 文件存放目录,生成html文件 // if ($this->createrHtmlDir($DOCUMENT_ROOT)) { if ($fp = fopen($this->pathCheck($DOCUMENT_ROOT).$filename, 'w')) { fwrite($fp, $content); fclose($fp); // 关闭缓冲区 // ob_end_clean(); } else { exit('生成 '.$this->pathCheck($DOCUMENT_ROOT).$filename.' 失败'); } } else { exit($DOCUMENT_ROOT.'目录不存在并且尝试建立失败!!'); } }
/** * check the path last character * @param string $str (path) * @return string */ function pathCheck($str) { return (preg_match('/\/$/',$str)) ? $str : $str.'/'; }
/** * delete the html file * @param $htmlfile * @return boolean */ function delHtml($htmlfile) { if (@unlink($this->data_path.$htmlfile.$this->suffix)) { return true; } else { return false; } } /** * 创建html文件目录 * @param $directory * @return boolean */ function createrHtmlDir($directory) { $target_dir = $directory; if (is_dir($target_dir)) { return true; } // 先判定系统是否为NT主机或php是否运行于安全模式 // if (strtoupper(substr(PHP_OS, 0, 3))=='WIN' || !get_cfg_var('safe_mode')) { $issafemode = false; $temp = ''; } else { $issafemode = true; $directory = $this->ftp_html_path; if ($this->www_root) { $this->sd('/'.$this->pathCheck($this->www_root)); } $temp = $this->pathCheck(@$_SERVER["DOCUMENT_ROOT"]); } $directory = $this->pathCheck($directory); $directorys = explode('/', $directory); for($i=0; $i<count($directorys)-1; $i++) { $temp .= $directorys[$i].'/'; //$temp = $this->pathCheck($temp);
// 如果目录不存在 // if (!is_dir($temp) && $directorys[$i]) { if ($issafemode) { // 安全模式采用 ftp 模式建目录 // perform current // $this->ftpCreateDir($this->nonce_dir,$directorys[$i]); } else { // 非安全模式采用传统模式建目录 // $oldmask = umask(0); mkdir($temp, 0777); umask($oldmask); } } else { $this->nonce_dir = $issafemode ? $directorys[$i] : $temp; } } if (is_dir($target_dir)) { return true; } else { return false; exit(); } }
/** * 测试目录是否可写 * @param dir 要检测的目录 * @return boolean */ function dirWriteAble($dir) { if(!is_dir($dir)) { @mkdir($dir, 0777); } if(is_dir($dir)) { if($fp = @fopen($dir.'/test.txt', 'w')) { @fclose($fp); @unlink($dir.'/test.txt'); $writeable = 1; } else { $writeable = 0; } } return $writeable; }
/** * 设置html文件的扩展名 * @param $suffix 文件扩展名 * @param void */ function setSuffix($suffix) { $this->suffix = '.'.$suffix; }
// function setFTPDir($filedir,$www_root=null) { $this->ftp_html_path = $filedir; $this->www_root = $www_root; }
}
$tohtml = new toHtml;
?> 欢迎大家使用,如转载请注明加蓝网 |
| |
| 【评论】【大 中 小】【打印】 【关闭】 |