PHP generates txt file example code introduction
This is a php code to generate txt files that a friend came over. This is just an example. I need to generate multiple txt file examples for him, but I think his code is a bit interesting, so I shared it.
Let’s first talk about the function of this php code to generate txt files. It must be to generate txt files. It’s a bit nonsense. Let’s not talk about other things. This php code can generate a txt file in the specified directory and put it in Write three lines of text into the txt file, which is defined in php.
The code of the example shared by Summer Blog is as follows:
<!doctype html> <html> <head> <meta charset="utf-8"> <title>无标题文档</title> </head> <body> <?php /** *1.前几天一哥们工作中他们领导让他写一个上生成文件的类:生成文件,文件类型支持:txt、html、csv、pdf、doc(或者docx)。 * *2.生成的内容是一张表格(像html中的table),参数为:生成文件的类型、生成内容的标题(数组),生成内容(数组,和标题相对应)。 */ /************************************************* * class name:createFile * description:create different type files * author:fenghuo * date:2013-11-12 ************************************************/ /** *3.我利用晚上的时间帮他就整理了一个生成txt的文件类. ***/ class createFile{ public $file_type; public $file_name; public $file_dir; /** * 构造函数:初始化生成文件的目录 */ public function __construct($file_dir){ $this->file_dir = $file_dir; } /** * 生成文件的入口函数 * @string $file_name 文件名 * @string $file_type 文件类型 * @array $title 生成内容的标题行 * @array $data 生成内容 */ 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; # 测试点 echo $fun,'--------------<br/>'; if( method_exists( $this,$fun)) { $file = $file_name.".".$file_type; $this -> $fun ($file,$title,$data); return true; }else{ return "NO!"; } } /** *生成txt类型文件 *@string $file 文件名 *@array $title 标题 *@array $data 内容 */ 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_encoding($data[$key][$i],'GBK',"UTF-8"); } $string .="\r\n"; } # 测试点 echo $this->file_dir.$file,'-----123---------<br/>'; $fp = fopen($this->file_dir.$file, "a+"); fwrite($fp,$string); fclose($fp); return true; } } //************************************** //测试 $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 "生成成功"; }else{ echo "生成失败"; } ?> </body> </html>
You need to create a new dev folder under the E drive, and then run it to see the effect. If the operation is successful, a test will be generated under the dev folder. txt file, and write the following content in it:
name sex age
tom boy 20
perry girl 20
The above is the entire content of this article, I hope it will be helpful to everyone learning PHP programming.
The above is an introduction to the example code for generating txt files in PHP, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Use Java's File.length() function to get the size of a file. File size is a very common requirement when dealing with file operations. Java provides a very convenient way to get the size of a file, that is, using the length() method of the File class. . This article will introduce how to use this method to get the size of a file and give corresponding code examples. First, we need to create a File object to represent the file we want to get the size of. Here is how to create a File object: Filef

To learn more about open source, please visit: 51CTO Hongmeng Developer Community https://ost.51cto.com Running environment DAYU200:4.0.10.16SDK: 4.0.10.15IDE: 4.0.600 1. To create an application, click File- >newFile->CreateProgect. Select template: [OpenHarmony] EmptyAbility: Fill in the project name, shici, application package name com.nut.shici, and application storage location XXX (no Chinese, special characters, or spaces). CompileSDK10, Model: Stage. Device

How to convert php blob to file: 1. Create a php sample file; 2. Through "function blobToFile(blob) {return new File([blob], 'screenshot.png', { type: 'image/jpeg' })} ” method can be used to convert Blob to File.

Convert basic data types to strings using Java's String.valueOf() function In Java development, when we need to convert basic data types to strings, a common method is to use the valueOf() function of the String class. This function can accept parameters of basic data types and return the corresponding string representation. In this article, we will explore how to use the String.valueOf() function for basic data type conversions and provide some code examples to

Method of converting char array to string: It can be achieved by assignment. Use {char a[]=" abc d\0efg ";string s=a;} syntax to let the char array directly assign a value to string, and execute the code to complete the conversion.

Use Java's File.renameTo() function to rename files. In Java programming, we often need to rename files. Java provides the File class to handle file operations, and its renameTo() function can easily rename files. This article will introduce how to use Java's File.renameTo() function to rename files and provide corresponding code examples. The File.renameTo() function is a method of the File class.

Replace characters (strings) in a string using Java's String.replace() function In Java, strings are immutable objects, which means that once a string object is created, its value cannot be modified. However, you may encounter situations where you need to replace certain characters or strings in a string. At this time, we can use the replace() method in Java's String class to implement string replacement. The replace() method of String class has two types:

Use java's File.getParent() function to get the parent path of a file. In Java programming, we often need to operate files and folders. Sometimes, we need to get the parent path of a file, which is the path of the folder where the file is located. Java's File class provides the getParent() method to obtain the parent path of a file or folder. The File class is Java's abstract representation of files and folders. It provides a series of methods for operating files and folders. Among them, get
