This article gives you a detailed description of how PHP generated TXT file title and content, here is a good example, you can feel the
The code is as follows: <?php /** *1. A couple of days ago, a guy at work, they led him to write a class that generates files: Makefile, file type support: TXT, HTML, CSV, PDF, doc (or docx). * *2. The resulting content is a table (like a table in HTML), with parameters: the type of the generated file, the title (array) of the generated content, the generated content (array, and the title corresponds). */ /************************************************* * Class name:createfile * Description: Create different type files * author:fenghuo * date:2013-11-12 ************************************* / /** *3. I used the night time to help him organize a file class that generates TXT . ***/ class createfile{ public $file _type ; public $file _name; public $file _dir; /** * constructor: Initializes the directory */ public function of the makefile __con struct ($file _dir) { $this->file_dir = $file _dir; } /** * Generate file Entry function * @string $file _name Component Name * @string $file _type file type * @array $title header row for generated content * @array $data generated content */ public function Create_file ($file _name, $file _type, $title, $data) { if (empty ($data)) { return false; } if (!empty ($title)) { if (count ($title)!= count ($data [0])) { return false; } } if ($ file_name = = "") { $file _name = $this->file_name; } if ($file _type = "") { $file _type = $t his->file_type; } $fun = ' mk_ '. $file _type; # test point Echo $fun, '--------------<br/> '; if (method_exists ($this, $fun)) { $file = $file _name. ". $file _type; $this-> $fun ($file, $title, $data); return true; }else{ return "no!"; } } /** * Generate txt type file * @string $file filename * @array $title title * @array $data content */ Public Function Mk_txt ($file, $title, $data) { $string = ""; if (!empty ($title)) { for ($i = 0; $i &l T Count ($title); $i + +) { $string. = '. Mb_convert_encoding ($title [$i], ' GBK ', ' UTF-8 '); } $string. = "RN"; } foreach ($data as $key => $var) { for ($i = 0; $i < count ($data [$key]); $i + +) { $string. = '. Mb_convert_encoding ($data [$key] [$i], ' GBK ', ' UTF-8 '); } $string. = "RN"; }& nbsp # test points Echo $this->file_dir. $file, '-----123---------<br/> '; $fp = fopen ($this->file_dir. $file , "A +"); fwrite ($fp, $string); fclose ($fp); return true; } } //** //test $dir = ' E:dev '; $file _name = "Test"; $file _type = "txt "; $title = Array (" name "," Sex "," age "); $data [] = Array (" Tom "," Boy ",); $data [] = Array (" Perry "," Girl ; $file = new CreateFile ($dir); $flag = $file-> create_file ($file _name, $file _type, $title, $data); nbsp if ($flag = = true) { echo "Build Success"; }else{ echo "Build Failure"; } ?>