Php txt file generation instance code introduction

Source: Internet
Author: User
Tags learn php learn php programming
This article mainly introduces the example code for generating txt files in php, which has some reference value. if you are interested, you can refer to the txt file code generated by a friend in php, this is just an instance. I need to generate multiple txt file instances for him, but I think his code is a bit interesting, so I will share it with him.

Let's talk about the functions of generating txt file code in php. it must be a bit of nonsense, 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.

The sample code for the summer blog is as follows:

 Untitled Document<? Php/*** 1. when a Buddy worked a few days ago, they asked him to write a file generation class: generate a file. the file types include txt, html, csv, pdf, doc (or docx ). ** 2. the generated content is a table (like a table in html). the parameter is the file type generated, the title of the generated content (array), and the generated content (array, corresponding to the title ). *//************************************* * ************ Class name: createFile * description: create different type files * author: fenghuo * date: *************************************** * ******** // ***** 3. I used the evening time to help him sort out a txt file class. * **/class createFile {public $ file_type; public $ file_name; public $ file_dir;/*** constructor: initialize the directory */public function _ construct ($ file_dir) {$ this-> file_dir = $ fi Le_dir ;} /*** file generation Portal function * @ string $ file_name file name * @ string $ file_type file type * @ array $ title: the title line of the content generated * @ 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; # 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 a txt file * @ string $ file name * @ array $ 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_encoding ($ 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 "failed to generate" ;}?>

You need to create a new dev folder under Drive E, and then run it to see the effect. after successful execution, a file named test.txt is generated under the dev folder and the following content is written into it:

Name sex age
Tom boy 20
Perry girl 20

The above is all the content of this article. I hope it will help you learn php programming.

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.