用include()和ob_get_contents( )方法 生成静态文件

本文介绍了使用PHP生成静态页面的方法,包括通过包含动态文件并将输出保存为静态HTML,以及使用函数实现这一过程。同时提供了具体的代码示例。

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

     1. 生成静态文件可以在打开缓冲区的前提下,用include()方法去包含要执行的动态文件,这样该动态文件就会在缓冲区中执行,执行完毕后的静态HTML代码就保存在缓冲区中,然后用ob_get_contents()方法去获取缓冲区中的内容,(注意:在获取缓冲区内容的前提是一定要打开缓冲区ob_start();),将这些内容保存到一个字符串中,再将该字符串写入到文件中即可。

<?php 
header("Content-type: text/html; charset=UTF-8"); 
 ob_start();//打开缓冲区
 include("admin.php");//包含php文件admin.php
 $str = ob_get_contents();//获取php文件里面的内容
 $fp = fopen("admin.html", "w");//创建tt.html静态文件
 fwrite($fp, $str);//将字符串$str写入tt.html中,tt.html 即为静态页文件
 ob_end_clean();//清空缓冲区内容并关闭缓冲区
 echo "success";
?>

 

   2.生成静态首页文件

为了方便生成静态页面,可以把生成静态页面的代码写入到一个函数中,createhtml()中,该函数接受两个参数:$sourcePage 是将执行的动态文件URL地址,$targetPage是生成的静态文件的文件名。

 <?php
 header("Content-type: text/html; charset=UTF-8"); 
 function createhtml ($sourcePage,$targetPage){
     ob_start();
     $str = file_get_contents($sourcePage);
     $fp = fopen($targetPage, "w")or die("打开文件".$targetPage."出错");
     fwrite($fp, $str);    //将字符串$str 写入目标文件中
     ob_end_clean();       //清空缓存区内容并且关闭缓冲区
     echo "success";
     fclose($fp);
 }
 createhtml("http://localhost/php/news/admin.php","index1.html");
?>
   file_get_contents(string $url)函数 ,如果该文件是动态网页文件,该参数必须是绝对URL地址,如不是相对URL地址。因为要执行一个动态网页文件,只能在浏览器地址栏中输入该文件的绝对URL,而不能输入相对的URL,否则该函数会把php文件的源代码(而不是执行后生成的HTML代码)作为返回的字符串。

 

转载于:https://www.cnblogs.com/xs-yqz/p/4957485.html

// Copyright (c) Orbbec Inc. All Rights Reserved. // Licensed under the MIT License. #include <stdio.h> #include <stdlib.h> #include <libobsensor/ObSensor.h> #include <libobsensor/ObSensor.h> #include "utils_c.h" #include "utils_types.h" void calculate_and_print_frame_rate(const ob_frame *frameset) { // Initialize variables static float color_count = 0; static float depth_count = 0; static uint64_t color_timestamp_last = 0; static uint64_t depth_timestamp_last = 0; if(color_timestamp_last == 0) { color_timestamp_last = ob_smpl_get_current_timestamp_ms(); } if(depth_timestamp_last == 0) { depth_timestamp_last = ob_smpl_get_current_timestamp_ms(); } ob_error *error = NULL; // Get color frame from frameset. const ob_frame *color_frame = ob_frameset_get_frame(frameset, OB_FRAME_COLOR, &error); CHECK_OB_ERROR_EXIT(&error); // calculate and print color frame rate if(color_frame != NULL) { color_count++; // Get timestamp from color frame. uint64_t color_timestamp_current = ob_smpl_get_current_timestamp_ms(); uint64_t duration = color_timestamp_current - color_timestamp_last; if(duration > 1000) { // calculate frame rate every second uint64_t index = ob_frame_get_index(color_frame, &error); CHECK_OB_ERROR_EXIT(&error); uint32_t width = ob_video_frame_get_width(color_frame, &error); CHECK_OB_ERROR_EXIT(&error); uint32_t height = ob_video_frame_get_height(color_frame, &error); CHECK_OB_ERROR_EXIT(&error); double frame_rate = color_count / (duration / 1000.0); printf("Color frame index: %d, width: %d, height: %d, frame rate: %.2f\n", (uint32_t)index, width, height, frame_rate); color_count = 0; color_timestamp_last = color_timestamp_current; } // delete color frame ob_delete_frame(color_frame, &error); CHECK_OB_ERROR_EXIT(&error); } // Get depth frame from frameset. const ob_frame *depth_frame = ob_frameset_get_frame(frameset, OB_FRAME_DEPTH, &error); CHECK_OB_ERROR_EXIT(&error); // calculate and print depth frame rate if(depth_frame != NULL) { depth_count++; // Get timestamp from depth frame. uint64_t depth_timestamp_current = ob_smpl_get_current_timestamp_ms(); uint64_t duration = depth_timestamp_current - depth_timestamp_last; if(duration > 1000) { // 1 seconds uint64_t index = ob_frame_get_index(depth_frame, &error); CHECK_OB_ERROR_EXIT(&error); uint32_t width = ob_video_frame_get_width(depth_frame, &error); CHECK_OB_ERROR_EXIT(&error); uint32_t height = ob_video_frame_get_height(depth_frame, &error); CHECK_OB_ERROR_EXIT(&error); double frame_rate = depth_count / (duration / 1000.0); printf("Depth frame index: %d, width: %d, height: %d, frame rate: %.2f\n", (uint32_t)index, width, height, frame_rate); depth_count = 0; depth_timestamp_last = depth_timestamp_current; } // delete depth frame ob_delete_frame(depth_frame, &error); CHECK_OB_ERROR_EXIT(&error); } } int main(void) { // Used to return SDK interface error information. ob_error *error = NULL; // Create a pipeline to manage the streams ob_pipeline *pipe = ob_create_pipeline(&error); CHECK_OB_ERROR_EXIT(&error); // Start Pipeline with default configuration (At default, the pipeline will start with the color and depth streams) ob_pipeline_start(pipe, &error); CHECK_OB_ERROR_EXIT(&error); printf("Streams have been started. Press 'ESC' key to stop the pipeline and exit the program.\n"); // Main loop, continuously wait for frames and print their index and rate. while(true) { char key = ob_smpl_wait_for_key_press(1); if(key == ESC_KEY) { break; } // Wait for frameset from pipeline, with a timeout of 1000 milliseconds. ob_frame *frameset = ob_pipeline_wait_for_frameset(pipe, 1000, &error); CHECK_OB_ERROR_EXIT(&error); // If frameset is NULL, timeout occurred, continue to next iteration. if(frameset == NULL) { continue; } // Get the color and depth frames from the frameset and calculate their frame rate. calculate_and_print_frame_rate(frameset); // delete frameset ob_delete_frame(frameset, &error); CHECK_OB_ERROR_EXIT(&error); } // Stop Pipeline ob_delete_pipeline(pipe, &error); CHECK_OB_ERROR_EXIT(&error); return 0; }
最新发布
05-15
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值