php生成txt文件實(shí)例代碼介紹
來(lái)源:易賢網(wǎng) 閱讀:789 次 日期:2016-08-25 15:31:45
溫馨提示:易賢網(wǎng)小編為您整理了“php生成txt文件實(shí)例代碼介紹”,方便廣大網(wǎng)友查閱!

這篇文章主要為大家詳細(xì)介紹了php生成txt文件實(shí)例代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

先說(shuō)下這個(gè) php 生成 txt 文件代碼都是什么功能吧,肯定是要生成 txt 文件的,有點(diǎn)廢話了,不說(shuō)其它的了,這個(gè) php 代碼可以生成指定目錄下的一個(gè) txt 文件,并在 txt 文件里面寫(xiě)入三行文字,這個(gè)是在 php 里面定義好的。

實(shí)例的代碼如下:

<!doctype html>

<html>

<head>

<meta charset="utf-8">

<title>無(wú)標(biāo)題文檔</title>

</head>

<body>

<?php

/** 

*1.前幾天一哥們工作中他們領(lǐng)導(dǎo)讓他寫(xiě)一個(gè)上生成文件的類(lèi):生成文件,文件類(lèi)型支持:txt、html、csv、pdf、doc(或者docx)。 

*2.生成的內(nèi)容是一張表格(像html中的table),參數(shù)為:生成文件的類(lèi)型、生成內(nèi)容的標(biāo)題(數(shù)組),生成內(nèi)容(數(shù)組,和標(biāo)題相對(duì)應(yīng))。 

*/

/************************************************* 

* class name:createFile 

* description:create different type files 

* author:fenghuo 

* date:2013-11-12 

************************************************/

/** 

*3.我利用晚上的時(shí)間幫他就整理了一個(gè)生成txt的文件類(lèi). 

***/

class createFile{ 

public $file_type; 

public $file_name; 

public $file_dir; 

/** 

* 構(gòu)造函數(shù):初始化生成文件的目錄 

*/

public function __construct($file_dir){ 

$this->file_dir = $file_dir; 

/** 

* 生成文件的入口函數(shù) 

* @string $file_name 文件名 

* @string $file_type 文件類(lèi)型 

* @array $title 生成內(nèi)容的標(biāo)題行 

* @array $data 生成內(nèi)容 

*/

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; 

# 測(cè)試點(diǎn) 

echo $fun,'--------------<br/>'; 

if( method_exists( $this,$fun)) 

$file = $file_name.".".$file_type; 

$this -> $fun ($file,$title,$data); 

return true; 

}else{ 

return "NO!"; 

/** 

*生成txt類(lèi)型文件 

*@string $file 文件名 

*@array $title 標(biāo)題 

*@array $data 內(nèi)容 

*/

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"; 

# 測(cè)試點(diǎn) 

echo $this->file_dir.$file,'-----123---------<br/>'; 

$fp = fopen($this->file_dir.$file, "a+"); 

fwrite($fp,$string); 

fclose($fp); 

return true; 

}

}

//************************************** 

//測(cè)試 

$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>

需要在 E 盤(pán)下面新建 dev 文件夾,然后進(jìn)行運(yùn)行即可看到效果,運(yùn)行成功,會(huì)在 dev 文件夾下面生成一個(gè) test.txt 的文件,并在里面寫(xiě)入如下的內(nèi)容:

name sex age

tom boy 20

perry girl 20

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家學(xué)習(xí)php程序設(shè)計(jì)有所幫助。

更多信息請(qǐng)查看網(wǎng)絡(luò)編程
易賢網(wǎng)手機(jī)網(wǎng)站地址:php生成txt文件實(shí)例代碼介紹
由于各方面情況的不斷調(diào)整與變化,易賢網(wǎng)提供的所有考試信息和咨詢(xún)回復(fù)僅供參考,敬請(qǐng)考生以權(quán)威部門(mén)公布的正式信息和咨詢(xún)?yōu)闇?zhǔn)!
關(guān)于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡(jiǎn)要咨詢(xún) | 簡(jiǎn)要咨詢(xún)須知 | 加入群交流 | 手機(jī)站點(diǎn) | 投訴建議
工業(yè)和信息化部備案號(hào):滇ICP備2023014141號(hào)-1 云南省教育廳備案號(hào):云教ICP備0901021 滇公網(wǎng)安備53010202001879號(hào) 人力資源服務(wù)許可證:(云)人服證字(2023)第0102001523號(hào)
云南網(wǎng)警備案專(zhuān)用圖標(biāo)
聯(lián)系電話:0871-65317125(9:00—18:00) 獲取招聘考試信息及咨詢(xún)關(guān)注公眾號(hào):hfpxwx
咨詢(xún)QQ:526150442(9:00—18:00)版權(quán)所有:易賢網(wǎng)
云南網(wǎng)警報(bào)警專(zhuān)用圖標(biāo)