php生成靜態檔案

fsl發表於2013-07-15

1,通用生成方法

//獲取檔案內容 
$content=file_get_contents("http://www.google.com/" );
$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,"http://google.com");
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);//將curl_exec()獲取的資訊以檔案流的形式返回
curl_setopt($ch,CURLOPT_HEADER,1);//是否將標頭檔案的資訊作為資料輸出

$output=curl_exec($ch);
if($output===FALSE){
echo "file error";
exit;
}

$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)."<br>";
//如果檔案存在並且生成時間大於1個小時刪除檔案
if(file_exists($page)&&$nowxx/60>60){ unlink($page); echo "操作完成"; }

 

 

相關文章