php生成html文件原理

fopen打开文件函数
fopen(路径和文件名,打开方式) R-只读 W-写入 A-读写
fread读取文件内容
fread(打开的文件,结束位置) 结束位置以字节为单位
filesize读取文件大小,字节为计量单位
filesize(路径和文件名)
fwrite写入文件内容
fwrite(路径和文件名,写入的内容)
fclose关闭打开的文件
fclose(路径和文件名)

<?php

$fp=fopen("tmp.htm","r");
$str=fread($fp,filesize("tmp.htm"));
$str=str_replace("{title}","Title",$str);
$str=str_replace("{content}","Content",$str);
fclose($fp);
$handle=fopen("news.html","w");
fwrite($handle,$str);
fclose($handle);
echo "生成成功!";
/*-------------------------www.iphpmysql.cn的分界线-------------------------------*/

$con=array(array('新闻标题','新闻内容'),array('新闻标题2','新闻内容2'));
foreach ($con as $id=>$val){
$title=$val[0];
$content=$val[1];
$path=$id.'.html';
$fp=fopen("tmp.htm","r");
$str=fread($fp,filesize("tmp.htm"));
$str=str_replace("{title}",$title,$str);
$str=str_replace("{content}",$content,$str);
fclose($fp);
$handle=fopen($path,"w");
fwrite($handle,$str);
fclose($handle);
echo "生成成功";
}?>

Leave a comment

Your comment

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word