Home > Backend Development > PHP Tutorial > PHP generates HTML files

PHP generates HTML files

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-07-29 09:02:49
Original
1043 people have browsed it

Most of the file functions have been touched before. Here are just a few important ones:

resourcefopen(string$filename ,string $mode[,bool$use_include_path= false[,resource$context]] ) //Open the file, there are read-only, write-only, read-write mode

int filesize(string $filename ) // Get the size of the file

intfwrite(resource$handle ,string $string[,int$length] ) //Write file

stringfread(resource$handle ,int $length) //Read files, you can read binary files

boolunlink(string$file name[, resource$context] ) //Delete files

More and more detailed content: http://php.net/
Simple type:

<?php
$in="tt.txt";
$fp=fopen($in,"r");
$get=fread($fp,filesize($in));
$fp=fopen("out.htm","w");
fwrite($fp,$get);
fclose($fp);
?>
Copy after login


Generate multiple files in a loop, use 3 txt files to generate 3 htm files (Equivalent to conversion):

Problems encountered:

Warning: fopen(michael jordan——Killer.htm): failed to open stream: Invalid argument

Encoding problem, the generated file was indeed successful, but The file name is garbled, so the returned result is that the path does not exist. Just change it to English.

<?php
$txtArray=array(array("t1.txt","fly.htm"),array("t2.txt","killer.htm"),
array("t3.txt","chamption.htm"));
foreach ($txtArray as $id=>$value){
    $filename=$value[0];
    $title=$value[1];
    $fp=fopen($filename,"r");
    $get=fread($fp,filesize($filename));
    $fp=fopen($title,"w");
    fwrite($fp,$get);
    unlink($filename); //删除源文件
}
fclose($fp);
?>
Copy after login

If you really want to name the htm file in Chinese, you can set the php file encoding to ASCII code:

<?php
$txtArray=array(array("t1.txt","michael jordan——飞翔.htm"),array("t2.txt","michael jordan——杀手.htm"),
array("t3.txt","michael jordan——首冠.htm"));
foreach ($txtArray as $id=>$value){
    $filename=$value[0];
    $title=$value[1];
    $fp=fopen($filename,"r");
    $get=fread($fp,filesize($filename));
    $fp=fopen($title,"w");
    fwrite($fp,$get);
    //unlink($filename); //删除源文件
}
fclose($fp);
?>
Copy after login

PHP 生成HTML文件

The above introduces how to generate HTML files with PHP, including the content. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template