ob缓存实例

实例一:

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. //ob缓冲  
  2. $filename="aa.html";  
  3. ob_start();  
  4. if(file_exists($filename))  
  5. {  
  6.     echo "缓存页面";  
  7.     echo "</br>";  
  8.     echo file_get_contents($filename);  
  9.   
  10. }  
  11. else  
  12. {  
  13.     echo "这是要缓存到页面的内容";  
  14.     $content=ob_get_contents();  
  15.     $fb=fopen($filename,'w');  
  16.     fwrite($fb,$content);  
  17.     fclose($fb);  
  18. }  
实例二:

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <?php  
  2. $filename="aa2.html";  
  3. if(file_exists($filename))  
  4. {  
  5.     echo "缓存中读取的";  
  6.     $files=file_get_contents($filename);  
  7.     echo $files;die;  
  8. }  
  9. else  
  10. {  
  11.     $dsn = "mysql:host=127.0.0.1;dbname=数据库";  
  12.     $db = new PDO($dsn, '数据库账号','数据库密码');  
  13.     $rs = $db->query("SELECT * FROM ecs_users");  
  14.     $rs->setFetchMode(PDO::FETCH_ASSOC);  
  15.     $arr = $rs->fetchAll();  
  16.     //首先要开启缓冲  
  17.     ob_start();  
  18. }  
  19. ?>  
  20. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  21. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">  
  22. <head>  
  23.     <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">  
  24.     <title>Document</title>  
  25. </head>  
  26. <body>  
  27. <table border="1">  
  28.     <tr>  
  29.         <td>用户ID</td>  
  30.         <td>用户邮箱</td>  
  31.         <td>用户姓名</td>  
  32.     </tr>  
  33.     <?php  
  34.     foreach($arr as $val)  
  35.     {  
  36.     ?>  
  37.     <tr>  
  38.         <td><?php echo $val['user_id']?></td>  
  39.         <td><?php echo $val['email']?></td>  
  40.         <td><?php echo $val['user_name']?></td>  
  41.     </tr>  
  42.     <?php  
  43.     }  
  44.     ?>  
  45. </table>    
  46. </body>  
  47. </html>  
  48. <?php  
  49. //获取ob缓冲的数据  
  50. $content=ob_get_contents();  
  51. //创建文件  
  52. $filename="aa2.html";  
  53. //写入文件  
  54. file_put_contents($filename,$content);  
  55. ?>  
实例一与实例二中

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. $fb=fopen($filename,'w');  
  2. fwrite($fb,$content);  
  3. fclose($fb);  
  4. 与  
  5. file_put_contents($filename,$content);  
  6. 意义一样,写入文件  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值