php生成HTML文件的應(yīng)用和原理筆記
來(lái)源:程序員人生 發(fā)布時(shí)間:2013-11-25 23:41:24 閱讀次數(shù):3889次
生成html頁(yè)面我們需要使用到的文件系統(tǒng)操作函數(shù)包括有fopen,fread,filesize,fwrite,fclose了,這些是基本要用到了,還像刪除,創(chuàng)建目錄之類的,下面我們來(lái)看看.
1.PHP部分文件操作函數(shù)。(fopen , fread , filesize , fwrite , fclose)
2.unlink() , mkdir() 刪除函數(shù)。
1.PHP部分文件操作函數(shù)
(1)fopen 打開(kāi)文件函數(shù)。 R / W / A
格式:fonpen(路徑和文件名,打開(kāi)方式);
(2)fread 讀取文件內(nèi)容。
格式:fread(打開(kāi)的文件,結(jié)束的位置);
(3)filesize 讀取文件大小,字節(jié)為計(jì)量單位。
格式:filesize(路徑和文件名);
(4)fwrite 寫(xiě)入文件內(nèi)容。
格式:fwrite(路徑和文件名,寫(xiě)入的內(nèi)容);
(5)fclose 關(guān)閉打開(kāi)的文件。
格式:fclose(路徑和文件名);
2.unlink(); mkdir(); 刪除函數(shù)
unlink(); 刪除文件函數(shù)
格式:unlink(路徑和文件);
mkdir(); 刪除目錄函數(shù)
格式:mkdir(路徑和目錄名);
實(shí)例操作,代碼如下:
- <?php
- $title = "新標(biāo)題";
- $content = "新內(nèi)容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 "生成成功";
- ?>
例,找到一個(gè)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__),0,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;
- }
- }
-
-
-
-
-
- ?>
生活不易,碼農(nóng)辛苦
如果您覺(jué)得本網(wǎng)站對(duì)您的學(xué)習(xí)有所幫助,可以手機(jī)掃描二維碼進(jìn)行捐贈(zèng)