Application and principles of generating HTML files using php

Source: Internet
Author: User

To generate an html page, we need to use the file system operation functions, including fopen, fread, filesize, fwrite, and fclose. These are basically used, such as deleting and creating directories, let's take a look.

1. PHP file operation functions. (Fopen, fread, filesize, fwrite, fclose)

2. unlink () and mkdir () delete functions.

-------------------------------------------------------------

1. PHP file operation functions
(1) fopen open the file function. R/W/
Format: fonpen (path and file name, open mode );

(2) fread reads the file content.
Format: fread (open file, end location );

(3) filesize: the size of the file to be read, measured in bytes.
Format: filesize (path and file name );

(4) fwrite writes the file content.
Format: fwrite (path and file name, written content );

(5) Close the opened file.
Format: fclose (path and file name );

-------------------------------------------------------------

2. unlink (); mkdir (); delete a function

Unlink (); delete a file Function
Format: unlink (path and file );

Mkdir (); delete a directory Function
Format: mkdir (path and directory name );

-------------------------------------------------------------

Instance operation:

The Code is as follows: Copy code

<? Php
$ Title = "New title ";
$ Content = "new content www. bKjia. c0m ";

$ Fp = fopen ("tmp.htm", "r"); // open the file in read-only mode.
$ Str = fread ($ fp, filesize ("tmp.htm"); // read the file content. Format: fread (open file, end position );.
$ Str = str_replace ("{title}", $ title, $ str); // Replace the path file content in the str variable with a new value.
$ Str = str_replace ("{content}", $ content, $ str );
Fclose ($ fp); // Replace the template content with the above.

$ Id = "hello ";
$ Path = $ id. '.htm ';
$ Handle = fopen ($ path, "w"); // The path to open the news in writing mode.
Fwrite ($ handle, $ str); // write the replaced content into the generated HTML file.
Fclose ($ handle );
Echo "generated successfully ";
?>

For example, find an html generation class

The Code is as follows: Copy code

<? Php

//--------------------------------------------------------------------------
// File name: html. class. php
// Description: www. bKjia. c0m: class for generating static pages
// Requirement: PHP5
//
// Copyright (C), cricket, 2013, All Rights Reserved.
//--------------------------------------------------------------------------

Class myHtml {

// Generate the html file path
Private $ html_dir = "./";
// Html file name
Private $ html_name;
// Generate the location name of the html file
Public $ path;
// Cache content
Private $ content;
// File handle
Private $ handle;
// Memory pointer
Private $ accesses;
// Constructor
Public function _ construct ($ html_dir = "", $ html_name = "")
{
$ This-> accesses ++;
// Create a folder if the file path does not exist
If (opendir ($ html_dir) = 0)
{
Mkdir ($ html_dir );
}

$ This-> html_dir = $ html_dir! = ""? $ Html_dir :"./";
$ This-> html_name = $ html_name! = ""? $ Html_name: substr (basename (_ FILE _), 0, strrpos (basename (_ FILE _), "."). ". html ";
$ This-> path = ($ this-> html_dir {strlen ($ this-> html_dir)-1} = "/")
? ($ This-> html_dir. $ this-> html_name) :( $ this-> html_dir. "/". $ this-> html_name );
Ob_start ();

}
// Destructor
Public function _ destruct ()
{
$ This-> accesses --;
Ob_end_clean ();
}
// Generate an html page
Function tohtml ()
{
$ This-> content = ob_get_contents ();
If (is_file ($ this-> path )){
@ Unlink ($ this-> path );
}
$ Handle = fopen ($ this-> path, "w ");
If (! Is_writable ($ this-> path )){
Return false;
}
If (! Fwrite ($ handle, $ this-> content )){
Return false;
}
Fclose ($ handle); // close the pointer
Return $ this-> path;
}
}
/*
$ Html = new myHtml ("./", "z.htm ");
Print "static page program ";
$ Html-> tohtml ();
*/
?>

 

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.