php 生成html类,php 简单生成html文件类

class mkHtml{

var $url;

var $contents;

var $path;

function __construct() {

$this->url ='http://www.111cn.net/';

$this->path ='../../default/';

}

function __destruct() {

unset( $this->url );

unset( $this->path );

unset( $this->contents );

}

function getHtml()

{

if(function_exists('file_get_contents') )

{

$this->contents =file_get_contents($this->url);

if( empty( $this->contents ) )

{

$this->curl();

}

}

else

{

$this->curl();

}

}

function curl()

{

$ch = curl_init();

$timeout = 10;

curl_setopt ($ch, CURLOPT_URL, $this->url);

curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);

curl_setopt ($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)");

curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);

$this->contents = curl_exec($ch);

curl_close($ch);

}

function saveHtml()

{

$this->getHtml();

if( !empty( $this->contents ) )

{

if( ! is_dir( $this->path ) || ! is_writable( $this->path ) )

{

echo '目录不存或不可写!';

}

else

{

$handle = fopen($this->path.'default.html','w+');

fwrite($handle,$this->contents);

fclose($handle);

echo '生成首页成功';

}

}

else

{

echo '未开通文件远程读取函数,请在php.ini中去了extension=php_curl.dll前面";"或设置allow_url_fopen=On';

}

}

}

//使用方法

$html = new mkHtml();

$html->saveHtml();

//www.111cn.net教程原创,转载注明

本文原创发布php中文网,转载请注明出处,感谢您的尊重!

Php 生成静态html文件 总结有如下三种方法 1,下面使用模版的一个方法! <?php $fp = fopen ("templets.html","a"); if ($fp){ $fup = fread ($fp,filesize("templets.html")); $fp2 = fopen ("html.shtml","w"); if ($fwrite ($fp2,$fup)){ $fclose ($fp); $fcolse ($fp2); die ("写入模板成功"); } else { fclose ($fp); die ("写入模板失败!"); } } ?> 简单的将模板写进一个文件中存为html. 2,按时间生成html文件名 <? $content = "这是一个以日期时间为文件名的静态生成网页的测试文件文件名格式一般为<font color=#ff0000>年月日时分秒.html</font>"; $date = date('YmdHis'); $fp = fopen (date('YmdHis') . '.html',"w");//本函数可用来打开本地或者远端的文件 'w' 开文件方式为写入,文件指针指到开始处,并将原文件的长度设为 0。若文件不存在,则建立新文件。 if (fwrite ($fp,$content)){//格式是.int fwrite(int fp(文件名), string string(内容), int [length](长度));本函数将字符串 string 写入文件资料流的指针 fp 上。若有指定长度 length,则会写入指定长度字符串,或是写到字符串结束。 fclose ($fp);//函数用来关闭已经打开的文件的指针 fp。成功返回 true,失败则返回 false。 die ("写入模板成功"); } else { fclose ($fp); die ("写入模板失败!"); } echo ($content); ?> 3,下面为转换文件名的一个方法 <?php $s_fname = "93e.php"; $o_fname = "93e.htm"; ob_end_clean(); ob_start(); include($s_fname); $length = ob_get_length(); $buffer = ob_get_contents(); $buffer = eregi_replace("r","",$buffer); ob_end_clean(); $fp = fopen($o_fname,"w+"); fwrite($fp,$buffer); fclose($fp); ?> 这样就可以把 93e.php转化为静态的HTML文件了 要注意的是待转换的文件里不能有 ob_end_clean();和 ob_start();语句。 且目录要有写权限。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值