1.PHP partial file Operation function. (fopen, Fread, FileSize, fwrite, fclose)
2.unlink (), mkdir () deletes the function.
-------------------------------------------------------------
1.PHP Partial file Operation function
(1) fopen open the file function. R/W/A
Format: Fonpen (path and file name, open mode);
(2) Fread read the contents of the file.
Format: fread (open file, end position);
(3) FileSize reads the file size, the byte is the unit of measure.
Format: filesize (path and filename);
(4) Fwrite writes the contents of the file.
Format: fwrite (path and file name, written content);
(5) Fclose closes the open file.
Format: fclose (path and filename);
-------------------------------------------------------------
2.unlink (); mkdir (); Delete function
Unlink (); Delete a file function
Format: Unlink (path and file);
mkdir (); Delete a directory function
Format: mkdir (path and directory name);
-------------------------------------------------------------
Instance action:
The code is as follows |
Copy Code |
<?php $title = "new title"; $content = "New content www.111cn.net"; $fp = fopen ("tmp.htm", "R"); Open the file in read-only mode. $str = Fread ($fp, FileSize ("tmp.htm")); Read the contents of the file, format: fread (open file, end of position);. $str = Str_replace ("{title}", $title, $STR); Replace the contents of a path file in a str variable with a value assignment $str = Str_replace ("{content}", $content, $STR); Fclose ($FP); The above is the replacement of the contents of the template. $id = "Hello"; $path = $id. '. htm '; $handle = fopen ($path, "w"); Write way to open a news path Fwrite ($handle, $STR); Write what you just replaced into the generated HTML file Fclose ($handle); echo "Build Success"; ?> |
example, find an HTML build class
The code is as follows |
Copy Code |
<?php // -------------------------------------------------------------------------- File name:html.class.php Description:www.111cn.net classes that generate static pages Requirement:php5 // Copyright (C), crickets, 2013, all Rights Reserved. //-------------------------------------------------------------------------- Class myhtml{ //Generate HTML file path Private $html _dir= "./" //html file name private $html _name; //Generate HTML file location name Public $path; //buffer content Private $content; //File handle private $handle; //Memory pointer private $accesses; & nbsp; //Constructor Public function __construct ($html _dir= "", $html _name= "") { $this->accesses++; //If the file path does not exist Create folder 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 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 pointer return $this->path; } } /* $html =new myhtml ("./", "z.htm"); Print "static page program"; $html->tohtml (); */ ?> |