About_PHP读写文件

1、PHP部分文件操作函数:
fopen ,fread ,filesize,fwrite,fclose 

2、unlink() rmdir() 删除函数

unlink() 删除文件函数:unlink(路径和文件名)

rmdir() 删除目录函数:rmdir(路径和目录名)

<meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/>
<?php

//$arr = array(array("新闻标题222","新闻内容222"),array("新闻标题333","新闻内容3333"));
//
//foreach($arr as $id=>$value){
//
//    $fp = fopen("tmp.htm","r");
//
//    $str = fread($fp,filesize("tmp.htm"));
//
//    fclose($fp);
//    $title = $value[0];
//    $content = $value[1];
//    $path = $id.".html";
//    $str = str_replace("{title}",$title,$str);
//    $str = str_replace("{content}",$content,$str);
//
//    $nfp = fopen($path,"w");
//    fwrite($nfp,$str);
//    fclose($nfp);
//
//}

$fp = fopen("tmp.htm","r");

$str = fread($fp,filesize("tmp.htm"));

fclose($fp);

$str = str_replace("{title}","新闻标题111",$str);
$str = str_replace("{content}","新闻内容111",$str);

$newFP = fopen("new.htm","w");
fwrite($newFP,$str);

fclose($newFP);
echo "写入 成功!";

?>

(1)  fopen 打开文件函数:fopen (路径和文件名,打开方式);

(2) fread 读取文件内容:fread (打开的文件,结束位置);

(3) filesize 读取文件大小,字节为计量单位:filesize (路径和文件名);

(4) fwrite 写入文件内容:fwrite (路径和文件名,写入的内容);

(5) fclose 关闭打开的文件:fclose (路径和文件名);

原文地址:https://www.cnblogs.com/a-moemiss/p/3753994.html