09/10/2018, 23:36
Tạo file HTML tĩnh với PHP động - Quá tuyệt
Đặt code này cuối trang
$html = ob_get_contents();
ob_end_clean();
WriteContent2File(str_replace(".php",'.html',__FIL E__), $html, false);
Biến $html là biến lưu nội dung thay vì xuất ra trình duyệt thì nó sẽ lưu vào biến đó, giờ chỉ việc save vào file nào đó.
Ở đây dùng hàm WriteContent2File hàm sưu tầm
function WriteContent2File($FileName, $strContent, $append = true)
{
if ($append)
{
$FileOpenedHandle = @fopen($FileName, "a");
}
else
{
$FileOpenedHandle = @fopen($FileName, "w");
}
if (@is_writable($FileName))
{
@fwrite($FileOpenedHandle, $strContent);
@fclose($FileOpenedHandle);
}
else
{
die("Error: The file $FileName does not allow to write.");
}
}
Thí dụ file test.php sẽ lưu thêm 1 file test.html
$html = ob_get_contents();
ob_end_clean();
WriteContent2File(str_replace(".php",'.html',__FIL E__), $html, false);
Biến $html là biến lưu nội dung thay vì xuất ra trình duyệt thì nó sẽ lưu vào biến đó, giờ chỉ việc save vào file nào đó.
Ở đây dùng hàm WriteContent2File hàm sưu tầm
function WriteContent2File($FileName, $strContent, $append = true)
{
if ($append)
{
$FileOpenedHandle = @fopen($FileName, "a");
}
else
{
$FileOpenedHandle = @fopen($FileName, "w");
}
if (@is_writable($FileName))
{
@fwrite($FileOpenedHandle, $strContent);
@fclose($FileOpenedHandle);
}
else
{
die("Error: The file $FileName does not allow to write.");
}
}
Thí dụ file test.php sẽ lưu thêm 1 file test.html
Bài liên quan
kiểm tra trước rồi tạo sau
còn về vấn đề ứng dụng cache cho forum ra file html. theo tui là không có gì sai. bạn có thể tạo cronjob 12h đêm empty cái folder chứa file cache mà.
IIS cũng có mod rewrite, bạn search trên Goo sẽ thấy, tôi có dùng thử nhưng nó ko có hiệu quả, lại phải trả tiền, remove cũng không được, nên chuyển sang xài Apache vì dùng PHP ko à.