php 批量生成html、txt文件解决方案方法

本文介绍了一种使用PHP从数据库批量生成HTML和TXT文件的方法。通过连接数据库,执行SQL查询,然后遍历查询结果,将数据填充到HTML模板中并生成对应的HTML文件,同时也生成了TXT文件。此方法适用于需要快速生成大量静态页面或文本文件的场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

首先建立一个conn.php的文件用来链接数据库

<?php
    $link = mysql_connect("mysql_host" , "mysql_user" , "mysql_password" )or die("Could not connect : " . mysql_error()); 
    mysql_query("set names utf8"); 
    mysql_select_db("my_database") or die("Could not select database");
?>

php 批量生成html

<?php
    require_once(“conn.php”);

    $query = "SELECT id,title,introduce FROM my_table";
    $result = mysql_query($query) or die("Query failed : " . mysql_error()); 

    /* 生成 HTML 结果 */
    while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
            
        $id=$row['id'];
        $title=$row['title'];
        $introduce=$row['introduce'];
        $path="html/$id.html";
        $fp=fopen("template.html","r"); //只读打开模板
        $str=fread($fp,filesize("template.html"));//读取模板中内容
        $str=str_replace("{title}",$title,$str);
        $str=str_replace("{introduce}",$introduce,$str);//替换内容
        fclose($fp);
        $handle=fopen($path,"w"); //写入方式打开新闻路径
        fwrite($handle,strip_tags($introduce)); //把刚才替换的内容写进生成的HTML文件
        fclose($handle);
        //echo "<a href=html/$id.html>生成成功</a>"."<br>";
    }

    /* 释放资源 */
    mysql_free_result($result);
    mysql_close($link);
?>

 

template.html文件内容:

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>{title}</title>
</head>

<body>
{introduce}
</body>
</html>

 

php 批量生成txt

 

<?php
    require_once(“conn.php”);

    $query = "SELECT kid,title,introduce FROM pro_courses";
    $result = mysql_query($query) or die("Query failed : " . mysql_error()); 

    /* 生成 txt 结果 */
    while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
            
        $id=$row['id'];
        $title=$row['title'];
        $introduce=$row['introduce'];
        $path="html/$id.txt";
        $handle=fopen($path,"w"); //写入方式打开新闻路径
        fwrite($handle,strip_tags($introduce)); //把刚才替换的内容写进生成的txt文件
        fclose($handle);
    }

    /* 释放资源 */
    mysql_free_result($result);
    mysql_close($link);
?>

参考:

https://www.10kuaijian.com
https://www.kuaijiandian.com
https://www.kuaijianjm.com
http://www.xaheimi.com
http://topcom.oicp.io
http://comcn.xicp.io
http://www.icoyun.com
http://www.icoyun.com/blog/

 

Php 生成静态html文件 总结有如下三种方法 1,下面使用模版的一个方法! <?php $fp = fopen ("templets.html","a"); if ($fp){ $fup = fread ($fp,filesize("templets.html")); $fp2 = fopen ("html.shtml","w"); if ($fwrite ($fp2,$fup)){ $fclose ($fp); $fcolse ($fp2); die ("写入模板成功"); } else { fclose ($fp); die ("写入模板失败!"); } } ?> 简单的将模板写进一个文件中存为html. 2,按时间生成html文件名 <? $content = "这是一个以日期时间为文件名的静态生成网页的测试文件文件名格式一般为<font color=#ff0000>年月日时分秒.html</font>"; $date = date('YmdHis'); $fp = fopen (date('YmdHis') . '.html',"w");//本函数可用来打开本地或者远端的文件 'w' 开文件方式为写入,文件指针指到开始处,并将原文件的长度设为 0。若文件不存在,则建立新文件。 if (fwrite ($fp,$content)){//格式是.int fwrite(int fp(文件名), string string(内容), int [length](长度));本函数将字符串 string 写入文件资料流的指针 fp 上。若有指定长度 length,则会写入指定长度字符串,或是写到字符串结束。 fclose ($fp);//函数用来关闭已经打开的文件的指针 fp。成功返回 true,失败则返回 false。 die ("写入模板成功"); } else { fclose ($fp); die ("写入模板失败!"); } echo ($content); ?> 3,下面为转换文件名的一个方法 <?php $s_fname = "93e.php"; $o_fname = "93e.htm"; ob_end_clean(); ob_start(); include($s_fname); $length = ob_get_length(); $buffer = ob_get_contents(); $buffer = eregi_replace("r","",$buffer); ob_end_clean(); $fp = fopen($o_fname,"w+"); fwrite($fp,$buffer); fclose($fp); ?> 这样就可以把 93e.php转化为静态HTML文件了 要注意的是待转换的文件里不能有 ob_end_clean();和 ob_start();语句。 且目录要有写权限。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值