This is a friend came over the PHP generated TXT file code, this is just an instance, I need to give him to generate multiple TXT file instances, but I think he this code a little meaning, so share it up.
First of all, this PHP generated TXT file code is what function, it is definitely to generate TXT file, a bit of nonsense, do not say anything else, this PHP code can generate a TXT file under the specified directory, and write three lines of text in the TXT file, this is defined in PHP.
Summer Blog Share the following example code is as follows:
<title>Untitled Document</title><?php/**. The other day, a buddy at work, they led him to write a class on the makefile: Generate files, file type support: TXT, HTML, CSV, PDF, doc (or docx). * * * *. The generated content is a table (like a table in HTML), with parameters such as the type of the generated file, the title (array) of the generated content, and the resulting content (array, and caption corresponds). *//************************************************* * Class Name:createfile * Description:create different type files * Author:fenghuo * date:2013-11-12 ************************************************//**. I used the night's time to help him tidy up a txt-generated file class. /class createfile{public $file _type, public $file _name, public $file _dir;/** * constructor: Initialize directory */public function __co for makefile NStruct ($file _dir) {$this->file_dir = $file _dir;}/** * Generate file Entry function * @string $file _name filename * @string $file _type File type * @array $title the header row of the generated content * @array $data generate 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 = $this->file_type;} $fun = ' mk_ '. $file _type; # Test point Echo $fun, '--------------
'; 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 < count ($title); $i + +) {$string. = '. Mb_convert_encoding ($title [$i], ' GBK ', "UTF -8 "); } $string. = "\ r \ n"; } foreach ($data as $key + $var) {for ($i = 0; $i < count ($data [$key]); $i + +) {$string. = ". Mb_convert_encodin G ($data [$key] [$i], ' GBK ', ' UTF-8 '); } $string. = "\ r \ n"; # test point Echo $this->file_dir. $file, '-----123---------
'; $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", 20); $data [] = Array ("Perry", "Girl", 20); $file = new CreateFile ($dir); $flag = $file Create_file ($file _name, $file _type, $title, $data); if ($flag = = True) {echo "generated successfully";} else{echo "Build Failed";}? >
You need to create a new dev folder below the E-drive and then run it to see the results, run successfully, generate a Test.txt file under the Dev folder, and write the following content in it:
Name Sex Age
Tom Boy 20
Perry Girl 20
The above is the whole content of this article, I hope that you learn PHP programming help.
The above describes the PHP generated TXT file instance code introduction, including the aspects of the content, I hope the PHP tutorial interested in a friend helpful.