PHP生成文件类型

本文介绍了一个PHP类,可以生成包括txt、html、csv、pdf和doc在内的多种文件类型,通过设置文件名、类型、标题和内容来实现文件的创建。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

资源相关插件下载:请点击链接 http://download.youkuaiyun.com/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 "生成失败,请检查";
}

?>

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值