用php把数据生成txt文件的使用方法
来源:互联网 发布:js 字符串分割 编辑:程序博客网 时间:2024/05/15 00:40
可以下载各个类型的文件,目前我只使用了下载txt文件,可以使用
这个是最牛逼的下载类,可以不是我写的。。。转载地址关了找不到了,对作者说声抱歉和感谢
function download($file){ //First, see if the file exists if (!is_file($file)) { die("<b>404 File not found!</b>"); } //Gather relevent info about file $len = filesize($file); $filename = basename($file); $file_extension = strtolower(substr(strrchr($filename,"."),1)); //This will set the Content-Type to the appropriate setting for the file switch( $file_extension ) { case "pdf": $ctype="application/pdf"; break; case "txt": $ctype="application/txt"; break; case "exe": $ctype="application/octet-stream"; break; case "zip": $ctype="application/zip"; break; case "doc": $ctype="application/msword"; break; case "xls": $ctype="application/vnd.ms-excel"; break; case "ppt": $ctype="application/vnd.ms-powerpoint"; break; case "gif": $ctype="image/gif"; break; case "png": $ctype="image/png"; break; case "jpeg": case "jpg": $ctype="image/jpg"; break; case "mp3": $ctype="audio/mpeg"; break; case "wav": $ctype="audio/x-wav"; break; case "mpeg": case "mpg": case "mpe": $ctype="video/mpeg"; break; case "mov": $ctype="video/quicktime"; break; case "avi": $ctype="video/x-msvideo"; break; //The following are for extensions that shouldn't be downloaded (sensitive stuff, like php files) case "php": case "htm": case "html": //case "txt": die("<b>Cannot be used for ". $file_extension ." files!</b>"); break; default: $ctype="application/force-download"; } //Begin writing headers header("Pragma: public"); header("Expires: 0"); header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); header("Cache-Control: public"); header("Content-Description: File Transfer"); //Use the switch-generated Content-Type header("Content-Type: $ctype"); //Force the download $header="Content-Disposition: attachment; filename=".$filename.";"; header($header ); header("Content-Transfer-Encoding: binary"); header("Content-Length: ".$len); @readfile($file); exit; }
我使用的是thinkphp框架,获取数据后,对数据进行拼接,保证格式,其中换行和tab使用\r\n,\t 注意一定要用双引号,单引号不解析,
public function totext(){$todayusermessage = M("Todayusermessage");$todayusermessageArr = $todayusermessage->select();$filename = './Public/Files/'.date('Y_m_d',time()).'.txt';$data = "序号\t姓名\t手机号\r\n";foreach($todayusermessageArr as $k=>$v){$data .= $k."\t".$v['name']."\t".$v['phone']."\r\n";}$file = file_put_contents($filename, $data);// 写好之后下载$re = download($filename);//检测是否设置文件名和文件是否存在 }
下载后如图
0 0
- 用php把数据生成txt文件的使用方法
- PHP生成TXT文件
- PHP生成TXT文件
- php 生成.txt文件
- PHP生成TXT的下载文件
- 把txt文件的数据导入到mysql中2
- 把txt文件的数据导入到mysql中2
- php 批量生成html、txt文件
- php生成txt文件标题及内容
- php生成txt文件标题及内容
- PHP生成TXT文件并自动下载
- PHP生成txt文件标题及内容
- PHP实现txt文件生成与下载
- C#把txt的数据导入excel,用逗号分隔
- ReadDataInTxtFileFor1024Bytes 读txt文件,把数据以1024K单位分隔在不同的文件中
- 把txt转换成pdf的广泛使用方法
- php将txt文件数据导入数据库
- php---从txt文件读取数据
- HashMap为什么是线程不安全的?
- Android通用流行框架大全
- 2017 最全的机器学习开源项目列表
- UIPageControl修改圆点图片及间距
- JVM机制及后台开发问题解决
- 用php把数据生成txt文件的使用方法
- [均摊 平衡树 || 线段树] Codeforces 438D #250 (Div. 1) D. The Child and Sequence
- SQL之触发器
- Android程序员学PHP开发(14)-whlie练习-PhpStorm
- textarea最多输入的长度
- HTML5 canvas专栏
- linux 开启mysql远程
- 关于PHP的数据库(Mysql)连接池
- 欢迎使用CSDN-markdown编辑器