PHP生成文件类型
2014-02-24 11:10
120 查看
资源相关插件下载:请点击链接 http://download.csdn.net/detail/u010253004/6954259
<?php
/**
1.编写一个类:生成文件,文件类型支持:txt、html、csv、pdf、doc(或者docx)。
*/
/*************************************************
* class name:createFile
* description:create different type files
* author:zyw
* date:2013-11-08
************************************************/
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;
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){
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";
}
$fp = fopen($this->file_dir.$file, "w+");
fwrite($fp,$string);
fclose($fp);
return true;
}
/**
*生成-----------------------------csv-----------------------------------------------------
*@string $file 文件名
*@array $title 标题
*@array $data 内容
*/
public function mk_csv($file,$title,$data){
if(!empty($title)){
for( $i = 0;$i < count( $title ); $i++ ){
$string .= ',' .mb_convert_encoding($title[$i],'GBK',"UTF-8");
}
$string .="\t\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 .="\t\n";
}
$fp = fopen($this->file_dir.$file, "w+");
fwrite($fp,$string);
fclose($fp);
return true;
}
/**
* 生成--------------------------doc-----------------------------------------
*/
function mk_doc($file,$title,$data){
ob_start();//打开输出控制缓冲
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 $string;
//echo $this->file_dir.$file."---------------------"."<br>";
$string = ob_get_contents(); //返回输出缓冲区的内容
ob_end_clean(); //清空缓冲区并关闭输出缓冲
$fp=fopen($this->file_dir.$file,"wb+");
fwrite($fp,$string);
fclose($fp);
}
/**
* 生成---------------------------html----------------------------
*/
function mk_html($file,$title,$data){
$string .= '<table border="1" width="200" >';
$string .= '<tr align="center">';
foreach ($title as $key => $item){
$item = mb_convert_encoding($item, 'gbk', 'utf-8');
$string .= '<td>' . $item . '</td>';
}
$string .= '</tr>';
foreach ($data as $line){
$string .= '<tr>';
foreach ($line as $key => &$item)
{
$item = mb_convert_encoding($item, 'gbk', 'utf-8');
$string .= '<td>' . $item . '</td>';
}
$string .= '</tr>';
}
$string .='</table>';
$fp = fopen($this->file_dir.$file, "w+");
fwrite($fp,$string);
fclose($fp);
return true;
}
/**
*------------------excel-----------------
*/
function mk_xls($file,$title,$data){
$string .= '<table border="1">';
$string .= '<tr>';
foreach ($title as $key => $item){
$item = mb_convert_encoding($item, 'gbk', 'utf-8');
$string .= '<td>' . $item . '</td>';
}
$string .= '</tr>';
foreach ($data as $line){
$string .= '<tr>';
foreach ($line as $key => &$item)
{
$item = mb_convert_encoding($item, 'gbk', 'utf-8');
$string .= '<td>' . $item . '</td>';
}
$string .= '</tr>';
}
$string .='</table>';
$fp = fopen($this->file_dir.$file, "w+");
fwrite($fp,$string);
fclose($fp);
return true;
}
/**
*-----------------------pdf-----------------------
*/
function mk_pdf($file,$title,$data){
require_once( './fpdf17/fpdf.php' );
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";
}
$pdf = new FPDF();
$pdf->SetFont('Arial','',20);
$pdf->AddPage();
$pdf->Cell(40,10,$string,5);
$pdf->Output();
}
}
//测试
$dir ='E:\dev\apache\htdocs\files\ ';
$file_name = "test";
$file_type = "txt";
$title = array("num","name","sex","age");
$data[] = array(1,"tom","boy",21);
$data[] = array(2,"perry","girl",20);
$file = new createFile($dir);
$flag = $file-> create_file($file_name,$file_type,$title,$data);
if($flag == true){
echo "成功,生成文件保存目录在 ".$dir."下";
}else{
echo "生成失败,请检查";
}
?>
<?php
/**
1.编写一个类:生成文件,文件类型支持:txt、html、csv、pdf、doc(或者docx)。
*/
/*************************************************
* class name:createFile
* description:create different type files
* author:zyw
* date:2013-11-08
************************************************/
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;
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){
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";
}
$fp = fopen($this->file_dir.$file, "w+");
fwrite($fp,$string);
fclose($fp);
return true;
}
/**
*生成-----------------------------csv-----------------------------------------------------
*@string $file 文件名
*@array $title 标题
*@array $data 内容
*/
public function mk_csv($file,$title,$data){
if(!empty($title)){
for( $i = 0;$i < count( $title ); $i++ ){
$string .= ',' .mb_convert_encoding($title[$i],'GBK',"UTF-8");
}
$string .="\t\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 .="\t\n";
}
$fp = fopen($this->file_dir.$file, "w+");
fwrite($fp,$string);
fclose($fp);
return true;
}
/**
* 生成--------------------------doc-----------------------------------------
*/
function mk_doc($file,$title,$data){
ob_start();//打开输出控制缓冲
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 $string;
//echo $this->file_dir.$file."---------------------"."<br>";
$string = ob_get_contents(); //返回输出缓冲区的内容
ob_end_clean(); //清空缓冲区并关闭输出缓冲
$fp=fopen($this->file_dir.$file,"wb+");
fwrite($fp,$string);
fclose($fp);
}
/**
* 生成---------------------------html----------------------------
*/
function mk_html($file,$title,$data){
$string .= '<table border="1" width="200" >';
$string .= '<tr align="center">';
foreach ($title as $key => $item){
$item = mb_convert_encoding($item, 'gbk', 'utf-8');
$string .= '<td>' . $item . '</td>';
}
$string .= '</tr>';
foreach ($data as $line){
$string .= '<tr>';
foreach ($line as $key => &$item)
{
$item = mb_convert_encoding($item, 'gbk', 'utf-8');
$string .= '<td>' . $item . '</td>';
}
$string .= '</tr>';
}
$string .='</table>';
$fp = fopen($this->file_dir.$file, "w+");
fwrite($fp,$string);
fclose($fp);
return true;
}
/**
*------------------excel-----------------
*/
function mk_xls($file,$title,$data){
$string .= '<table border="1">';
$string .= '<tr>';
foreach ($title as $key => $item){
$item = mb_convert_encoding($item, 'gbk', 'utf-8');
$string .= '<td>' . $item . '</td>';
}
$string .= '</tr>';
foreach ($data as $line){
$string .= '<tr>';
foreach ($line as $key => &$item)
{
$item = mb_convert_encoding($item, 'gbk', 'utf-8');
$string .= '<td>' . $item . '</td>';
}
$string .= '</tr>';
}
$string .='</table>';
$fp = fopen($this->file_dir.$file, "w+");
fwrite($fp,$string);
fclose($fp);
return true;
}
/**
*-----------------------pdf-----------------------
*/
function mk_pdf($file,$title,$data){
require_once( './fpdf17/fpdf.php' );
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";
}
$pdf = new FPDF();
$pdf->SetFont('Arial','',20);
$pdf->AddPage();
$pdf->Cell(40,10,$string,5);
$pdf->Output();
}
}
//测试
$dir ='E:\dev\apache\htdocs\files\ ';
$file_name = "test";
$file_type = "txt";
$title = array("num","name","sex","age");
$data[] = array(1,"tom","boy",21);
$data[] = array(2,"perry","girl",20);
$file = new createFile($dir);
$flag = $file-> create_file($file_name,$file_type,$title,$data);
if($flag == true){
echo "成功,生成文件保存目录在 ".$dir."下";
}else{
echo "生成失败,请检查";
}
?>
相关文章推荐
- php生成静态文件的多种方法分享
- yii phpexcel自动生成文件保存到服务器上
- php 导出csv类型表格文件
- php 静态文件生成类
- PHP 文件操作类(创建文件并写入) 生成日志
- 【laravel框架】compile.php文件的生成方法
- PHP 使用 PHPExcel 库生成 Excel 文件
- php 生成xml文件
- php中使用zendstudio 12为soapserver生成wsdl文件
- PHP四种文件载入类型详解
- 如何解决PHP生成UTF-8编码的CSV文件用Excel打开乱码的问题
- php生成 php配置文件
- fpdf 在php中生成pdf文件时如何使用自定义字库.
- php上传文件类型大全
- 怎么用PHP在HTML中生成PDF文件
- python生成CMPL16类型随机定标测试数据并输出到文件
- PHP 生成 csv 文件时乱码解决
- PHP实现生成vcf vcard文件功能类定义与使用方法详解【附demo源码下载】[原创]_php技巧_脚本之家
- PHP在线生成txt文件下载
- 利用PHP实现智能文件类型检测的实现代码