Summary of some methods for generating static html files with PHP_PHP Tutorial
WBOYOriginal
2016-07-13 10:43:33824browse
Using PHP to generate static files, what we use most is to replace them in the form of templates. For example, I define {A} as a tag. In PHP, I only need to use fopen to read the template file, and then use replace to replace {A}. Just generate a static html file instance for PHP.
1, here is a way to use templates!
The code is as follows
Copy code
代码如下
复制代码
$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 ("写入模板失败!");
}
}
?>
} else {
fclose ($fp);
die ("Failed to write template!");
}
}
?>
Simply write the template into a file and save it as html.html
2. Generate html file name according to time
The code is as follows
Copy code
<🎜>
$content = "This is a test file for statically generated web pages with date and time as the file name, <🎜>
The file name format is generally year, month, day, hour, minute and second.html";
$date = date('YmdHis');
$fp = fopen (date('YmdHis') . '.html',"w");
//This function can be used to open local or remote files 'w'. The file opening method is writing,
The file pointer points to the beginning and the length of the original file is set to 0. If the file does not exist,
A new file is created.
if (fwrite ($fp,$content)){
//The format is .int fwrite(int fp(file name), string string(content),
int [length](length)); This function writes the string string to the pointer fp of the file data stream.
If length is specified, the string of the specified length will be written, or written to the end of the string.
fclose ($fp);//The function is used to close the pointer fp of the opened file.
Returns true on success, false on failure.
die ("Write template successfully");
} else {
fclose ($fp);
die ("Failed to write template!");
}
echo ($content);
?>
3. The following is a method for converting file names
In this way, 93e.php can be converted into a static HTML file. It should be noted that there cannot be ob_end_clean(); and ob_start(); statements in the file to be converted, and the directory must have write permission.
The above three methods all generate html pages that need to be updated every time. Next, we can use dynamic page caching technology to instantiate html+php pages. This method is better than the above
First set up the .htaccess file to convert dynamically called parameters into static HTML URL addresses. For example, forward files in the post directory to the wp-post.php file in the root directory. Add statements similar to:
if (file_exists(HTML_FILE))
{
$lcft = filemtime(HTML_FILE);
if (($lcft + 3600) > time()) // Determine whether the last HTML file generated was more than 1 hour ago. If not, directly output the file content
{
echo(file_get_contents(HTML_FILE));
exit(0);
}
}
After is the existing PHP code, and then add the following PHP code at the end of the current code:
Okay, then check your static HTML page. If a comment line appears at the end of the page, it means that the static HTML file has been successfully created
http://www.bkjia.com/PHPjc/633160.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/633160.htmlTechArticleUsing PHP to generate static files. What we use most is to replace the generated files in the form of templates. For example, I put {A } is defined as a tag in php. Just use fopen to read the template file, and then use repla...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn