In the company's third week, the group is making a shopping platform, in order to improve the speed of access, the general site will have a lot of static pages, so that only once access to the database can no longer read the data. Can never think of is to generate static page This task leader gave me to do, hey ~ hehe, generate static page This kind of thing I always think is tall on the thing Ah, how will give me this new bird to do?! But I have to do it, if the delay is not very face ....
The first job is to use a universal search engine on the internet to find a variety of predecessors experience Ah, later looked at the discovery is actually very simple ah, not how inscrutable is simply the use of the PHP file class, and then write a function to generate a file, in the need to generate static files in the place to reference the function OK.
First introduce your own file class:
<?php/** * File processing class */class files{private $resource = null;//file resource handle function __construct ($fileName, $mode = ' R ') {$dirName c3/>= dirname ($fileName);//file path $basename = BaseName ($fileName);//filename//Check and Create folder Self::mkdir ($dirName); $this Resource = fopen ($fileName, $mode. ' B '); if ($this->resource) {flock ($this->resource,lock_ex);//Lock}}// File Write function Public function write ($content) {$worldsnum = fwrite ($this->resource, $content); return Is_bool ($worldsnum)? False: $worldsnum;}}
Write a function to generate a static file:
Private Function writehtml ($path, $content) {if (! class_exists (' Files ')) {$this-load->file (APPPATH. ' Libraries /files '. EXT);//I use the CI framework, introduce file class here to make a slight change $f = new files ($path, ' w+ '); $res = $f->write ($content); $f->save ();}
Finally call the above function where you need to generate a static page:
function crativehtml{$url = "php dynamic file path"; $content = file_get_contents ($url);//Get File Contents $this-_writefile ($ Path. ' File name. html ', $content);}
Just like this, a static file is generated, and the last file can be written in multiple places to generate a different static file 、、、
Do summarize after: will be not difficult, difficult not ~ High code nothing is inscrutable. Hey ~ Oh hehe,,