文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>历遍文件生成静态文件

历遍文件生成静态文件

时间:2008-02-22  来源:qsbaq

新写的代码,拿給大家分享!
本人新手,代码写的不行。
请大家多多包涵~

公司要做这种傻傻的页面,可是图片很多。
所以写了个简单的程序
代码中的i,j是2次循环
恰好我把图片也命名成ij.jpg的格式,所以就懒得打进数据库了。
根据workimg里面的jpg图片名字命名也就是ij.jpg的格式

其实也没什么难的,关键就是str_replace这个函数跟读写文件的操作。
o(∩_∩)o...大家注意理清思路哦

tohtml.php

<?PHP
require_once('../fun.inc.php');
$filename=lstree("../workimg/","jpg");
//print_r($filename);


$nums=count($filename);         //这里是统计共有多少个文件
//print_r($nums)


for($i=0;$i<$nums;$i++)
{            //下面的15是../workimg/ + jpg 的字符长度
$img=substr($filename[$i],11,strlen($filename[$i])-15);  
//print_r($img);


    $len=strlen($img);
    $toseries=substr($img,0,$len-1);
    $row=substr($img,$len-1,$len)-1;
    $seriesDetail = getDetailBySeriesId($toseries,'seriesDetail');
    $str="<strong>P".$toseries." ".($row+1)."-".$seriesDetail[$row]->name."</strong>";


    //读取模板文件
    $tmpfile="temp.html";
    $fp = fopen ($tmpfile,"r");
    $content = fread ($fp,filesize ($tmpfile));
    $content = str_replace ('<{$str}>',$str,$content);
    $content = str_replace ('<{$img}>',$img,$content);
    fclose($fp);
    //print_r($content);

    
    $tofile = $img.".htm";
    $handle = fopen ($tofile,"w"); //打开文件指针,创建文件


//    //////// 检查文件是否被创建且可写

//   
    if (!is_writable ($tofile)){
     die ("File:".$tofile."can not write!");
    }
    if (!fwrite ($handle,$content)){ //将信息写入文件

     die ("Create File".$tofile."Lost!");
    }
    fclose ($handle); //关闭指针

    
    echo $filename[$i] ." to ".$img.".htm ===> OK !"."<br>";
}        
?>


fun.inc.php

<?PHP
require_once('conn.inc.php');
function getSeriesById($id,$tblname)
{
    $sql="select * from `$tblname` where id='$id'";
    $res=mysql_query($sql);
    while($row=mysql_fetch_object($res))
    return $row;
}
function getDetailBySeriesId($toseriesid)
{
    $sql="select * from seriesDetail where toseriesid='$toseriesid'";
    $res=mysql_query($sql);
    while($row=mysql_fetch_object($res))
    {
        $rows[] =$row;
    }
    return $rows;
}
function countDetailIds($id)
{
    $sql="select id from seriesDetail where toseriesid='$id'";
    $res=mysql_query($sql);
    return mysql_num_rows($res);
}
function lstree($path,$str="")
{
    $list = array();
    foreach( glob( $path ."*.".$str) as $item )
    {
        if( is_dir($item ) )
        {
            $list = array_merge( $list , lstree( $item ) );
        }
        else
        {
            $list[] = $item;
        }
    }
    
    return $list;
}
?>


temp.html

<table border="0" cellpadding="0" cellspacing="0" width="1000" align="center">
  <tr>
    <td><table width="1000" border="0" cellspacing="0" cellpadding="0">
        <tr>
          <td width="16">&nbsp;</td>
          <td width="320" bgcolor="#77131E"><a href="#"><img src="../images/ADKungFu_logo.jpg" width="320" height="64" border="0" /></a></td>
          <td width="648" align="right" valign="top" bgcolor="#77131E">&nbsp;</td>
          <td width="16">&nbsp;</td>
        </tr>
      </table></td>
  </tr>
</table>
<br />
<bg />
<table align="center" width="967">
<tr>
<td class="boxtxt">
<{$str}>
</td>
</tr>
<tr>
<td style="padding-left:50px"><img src="../workimg/<{$img}>.jpg" /></td>
</tr>
<tr>
<td class="boxtxt">
<{$str}>
</td>
</tr>
</table>

相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载