生成html页面我们需要使用到的文件系统操作函数包括有fopen,fread,filesize,fwrite,fclose了,这些是基本要用到了,还像删除,创建目录之类的,下面我们来看看. 1.PHP部分文件操作函数。(fopen,fclose) 2.unlink(),mkdir() 删除函数。 1.PHP部分文件操作函数 (1)fopen 打开文件函数。 R / W / A 格式:fonpen(路径和文件名,打开方式); (2)fread 读取文件内容。 格式:fread(打开的文件,结束的位置); (3)filesize 读取文件大小,字节为计量单位。 格式:filesize(路径和文件名); (4)fwrite 写入文件内容。 格式:fwrite(路径和文件名,写入的内容); (5)fclose 关闭打开的文件。 格式:fclose(路径和文件名); 2.unlink(); mkdir(); 删除函数 unlink(); 删除文件函数 格式:unlink(路径和文件); mkdir(); 删除目录函数 格式:mkdir(路径和目录名); 实例操作,代码如下: - <?php
- $title = "新标题";
- $content = "新内容www.phpfensi.com";
- $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);
- $id = "hello";
- $path = $id . '.htm';
- $handle = fopen($path, "w");
- fwrite($handle, $str);
- fclose($handle);
- echo "生成成功";
- ?>
例,找到一个html生成类,代码如下: - <?php
-
-
-
-
-
-
-
- class myHtml{
-
- private $html_dir="./";
-
- private $html_name;
-
- public $path;
-
- private $content;
-
- private $handle;
-
- private $accesses;
-
- public function __construct($html_dir="",$html_name="")
- {
- $this->accesses++;
-
- if(opendir($html_dir)==0)
- {
- mkdir($html_dir);
- }
- $this->html_dir=$html_dir!=""?$html_dir:"./";
- $this->html_name=$html_name!=""?$html_name:substr(basename(__FILE__),strrpos(basename(__FILE__),".")).".html";
- $this->path= ($this->html_dir{strlen($this->html_dir)-1}=="/")
- ?($this->html_dir.$this->html_name):($this->html_dir."/".$this->html_name);
- ob_start();
- }
-
- public function __destruct()
- {
- $this->accesses--;
- ob_end_clean();
- }
-
- function tohtml()
- {
- $this->content=ob_get_contents();
- if (is_file ($this->path)){
- @unlink ($this->path);
- }
- $handle = fopen ($this->path,"w");
- if (!is_writable ($this->path)){
- return false;
- }
- if (!fwrite ($handle,$this->content)){
- return false;
- }
- fclose ($handle);
- return $this->path;
- }
- }
-
-
-
-
-
- ?>
(编辑:李大同)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|