您的位置:首页 > 编程语言 > PHP开发

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 "生成失败,请检查";

}

?>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息