PHP three ways to generate static HTML files

Source: Internet
Author: User
    1. $fp = fopen ("templets.html", "a");
    2. if ($fp) {
    3. $fup = Fread ($fp, FileSize ("templets.html"));
    4. $FP 2 = fopen ("html.shtml", "w");
    5. if ($fwrite ($fp 2, $fup)) {
    6. $fclose ($FP);
    7. $fcolse ($fp 2);
    8. Die ("Write template Success");
    9. } else {
    10. Fclose ($FP);
    11. Die ("Write template failed!");
    12. }
    13. }
    14. ?>
Copy Code

Write the template into a file and save it as html.html.

Method 2, generate HTML file names by time

    1. $content = "This is a test file of a statically generated Web page with datetime as the file name, and the file name format is usually date and time. html";
    2. $date = Date (' Ymdhis ');
    3. $fp = fopen (the date (' Ymdhis '). '. html ', ' W ');//This function can be used to open local or remote file ' W ' Open file method for writing, the file pointer refers 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.
    4. if (fwrite ($FP, $content)) {//the format is. int fwrite (int fp (file name), string string (contents), int [length]), and this function writes string strings to the file stream's pointer f P on. If the length is specified, the specified length string is written, or the end of the string is written.
    5. Fclose ($FP);//function to close a pointer to an already opened file FP. The success returns True, and False if it fails.
    6. Die ("Write template Success");
    7. } else {
    8. Fclose ($FP);
    9. Die ("Write template failed!");
    10. }
    11. Echo ($content);
    12. ?>
Copy Code

Method 3, convert the file name

    1. $s _fname = "93e.php";

    2. $o _fname = "93e.htm";
    3. Ob_end_clean ();
    4. Ob_start ();
    5. Include ($s _fname);
    6. $length = Ob_get_length ();
    7. $buffer = Ob_get_contents ();
    8. $buffer = Eregi_replace ("R", "", $buffer);
    9. Ob_end_clean ();

    10. $fp = fopen ($o _fname, "w+");

    11. Fwrite ($fp, $buffer);
    12. Fclose ($FP);
    13. ?>

Copy Code

This allows the 93e.php to be converted into a static HTML file. Note: You cannot have Ob_end_clean (), and Ob_start () statements in the file to be converted. The directory must have write permissions. You may be interested in the article: three ways to generate static pages in PHP and the code in detail PHP generates static page functions (php2html) example PHP method for generating static pages (three functions) The template of PHP generated static files and cache PHP write a static page generated on the class virtual host on a regular basis to automatically generate static page method PHP two ways to generate static files PHP generate static HTML file in a detailed tutorial php generation of Smarty Static page method

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.