php生成靜態檔案

2022-04-02 05:22:37 字數 1273 閱讀 7187

1,通用生成方法

//

獲取檔案內容

$content=file_get_contents("");

$id=110;

$filename="$id.html"; //

設定靜態檔案路徑及檔名

if(file_exists($filename)) unlink($filename); //

檢查是否存在舊檔案,有則刪除

$fp = fopen($filename, 'w'); //

寫入檔案

fwrite($fp, $content

); echo "is ok";

2,使用curl方法生成

$ch=curl_init();//

初始化curl_setopt($ch,curlopt_url,"");

curl_setopt(

$ch,curlopt_returntransfer,1);//

將curl_exec()獲取的資訊以檔案流的形式返回

curl_setopt($ch,curlopt_header,1);//

是否將標頭檔案的資訊作為資料輸出

$output=curl_exec($ch

);if($output===false

)$info=curl_getinfo($ch

);curl_close(

$ch);//

釋放curl控制代碼

$page=time().".html";//

開始構建相應頁面

//if(file_exists($page)) unlink($page); //檢查是否存在舊檔案,有則刪除

$tp = fopen($page, "a+");

fwrite($tp, $output

);fclose($tp

);header("location:$page");//

轉向生成頁面

3,根據檔案生成時間判斷是否需要再次生成

$page="110.html";

$a=filemtime($page

);$now=time

();$nowxx=$now-$a

;echo "time:".date("y-m-d h:i:s",$now)."-".date("y-m-d h:i:s",$a)."

";//如果檔案存在並且生成時間大於1個小時刪除檔案

if(file_exists($page)&&$nowxx/60>60)

PHP生成靜態檔案之真靜態

下面思路和 是在demo實現,非框架 實現思路 舉個例子 例如我們要把某個商品的詳情存入靜態頁面 html 獲取商品id 如果對應的靜態頁面存在否則 模板檔案 需要新建乙個page.html檔案 gid title price content 實現 需要新建乙個html 存放模板檔案的資料夾 hea...

PHP生成普通的靜態檔案

class file 生成靜態快取 param string key 快取檔名 param array value 快取內容 param string path 快取路徑 public function cachedata key,value path 生成快取檔案 dir dirname file...

PHP生成靜態html檔案的方法

1.php 1,下面使用模版的乙個方法 fp fopen templets.html a if fp else 簡單的將模板寫進乙個檔案中存為html.html 2,按時間生成html檔名 content 這是乙個以日期時間為檔名的靜態生成網頁的測試檔案,檔名格式一般為年月日時分秒.html dat...