PHP to generate TXT file title and content method

Source: Internet
Author: User

  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"; }   ?>   
Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.