產生html頁面我們需要使用到的檔案系統操作函數包括有fopen , fread , filesize , fwrite , fclose了,這些是基本要用到了,還像刪除,建立目錄之類的,下面我們來看看。
1.PHP部分檔案操作函數。(fopen , fread , filesize , fwrite , 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(路徑和目錄名);
-------------------------------------------------------------
執行個體操作:
| 代碼如下 |
複製代碼 |
|
$title = "新標題";
$content = "新內容www.bKjia.c0m";
$fp = fopen("tmp.htm", "r"); //開啟檔案,以唯讀方式。
$str = fread($fp, filesize("tmp.htm")); //讀取檔案內容,格式:fread(開啟的檔案,結束的位置);。
$str = str_replace("{title}", $title, $str); //將str變數中的路徑檔案內容替換掉重新賦值
$str = str_replace("{content}", $content, $str);
fclose($fp); //以上為替換模板的內容。
$id = "hello";
$path = $id . '.htm';
$handle = fopen($path, "w"); //寫入方式開啟新聞路徑
fwrite($handle, $str); //把剛才替換的內容寫進產生的HTML檔案
fclose($handle);
echo "產生成功";
?> |
例,找到一個html產生類
| 代碼如下 |
複製代碼 |
|
// --------------------------------------------------------------------------
// File name : html.class.php
// Description : www.bKjia.c0m產生靜態頁面的類
// Requirement : PHP5
//
// Copyright(C), 蟋蟀, 2013, All Rights Reserved.
//--------------------------------------------------------------------------
class myHtml{
//產生html檔案路徑
private $html_dir="./";
//html檔案名稱
private $html_name;
//產生html檔案的位置名稱
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();
}
//產生html頁面
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;
}
}
/*
$html=new myHtml("./","z.htm");
print "靜態頁面程式";
$html->tohtml();
*/
?> |
http://www.bkjia.com/PHPjc/632638.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/632638.htmlTechArticle產生html頁面我們需要使用到的檔案系統操作函數包括有fopen , fread , filesize , fwrite , fclose了,這些是基本要用到了,還像刪除,建立目錄之類...