php文件操作及html生成

来源:互联网 发布:家装diy设计软件 编辑:程序博客网 时间:2024/06/05 15:44
1、PHP部分文件操作函数
  
(1)  fopen 打开文件函数
(2)  fread 读取文件内容
(3)  filesize 读取文件大小,字节为计量单位
(4)  fwrite 写入文件内容
(5)  fclose 关闭打开的文件

2、unlink()  rmdir() 删除函数
unlink() 删除文件函数  
rmdir() 删除目录函数

3、效果图:
html修改前:
php文件操作及html生成 - 行也思君 - 行也思君
html修改后:
 
php文件操作及html生成 - 行也思君 - 行也思君
 生成的html文件:
php文件操作及html生成 - 行也思君 - 行也思君
 

<?php   $con=array(array('news1','hello world1'),array('news2','hello world2'));   foreach($con as $id => $val)   {   $title=$val[0];   $content=$val[1];   $path=$id.'.htm';//生成新文件      $fp=fopen("tmp.htm","r");//只读打开模板   $str=fread($fp,filesize("tmp.htm"));//读取模板中的内容   $str=str_replace("{title}","new title",$str);   $str=str_replace("{content}","file operator",$str);//替换文件内容   fclose($fp);      $handle=fopen($path,"w");//写入方式打开路径   fwrite($handle,$str);//写入新生成的html文件   fclose($handle);   echo "success\n";   //unlink($path);//删除文件   }?>



原创粉丝点击