& Lt ;? Php *** a few days ago, when a buddy was working, they asked him to write a file generation class: generate a file. the file type can be: txt, html, csv, pdf, doc (or docx ). ** 2. the generated content is a table (like a table in html). the parameter is: file type and file generation.
File_dir = $ file_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" ;}?>