php fputcsv() 函数csv数据读写数据库文件代码
发布:smiling 来源: PHP粉丝网 添加日期:2014-09-08 11:18:33 浏览: 评论:0
fputcsv() 函数用于将数据格式为csv格式,以便写入文件或者数据库.
1.将字符串写入csv文件中,代码如下:
- $test_array = array(
- array("111","sdfsd","sdds","43344","rrrr"),
- array("sssssssss","gdfgfd","232323","wwewe","dsfds"),
- array("fgfg","e4343","dsfds","w2332","xcvxc"),
- array("11212","2323","344343","344343","rerreer"),
- array("fds","43344444","33333333","ttttttt","gggggggggggg"),
- array("kdfs","dsfdsfds","wewewe","sdsdddddddd","wwwwwwwwwww")//开源代码phpfensi.com
- );
- $file = fopen("test.csv","w") or die("Can't Open test.csv");
- foreach($test_array as $line_array)
- {
- $isSuccess = fputcsv($file,$line_array);
- print $isSuccess."<br>";
- if($isSuccess===false)
- {
- die("Can't write csv line".$line_array);
- }
- }
- fclose($file) or die("Can't close file test.csv.");
fputcsv()函数返回所写入行的字符的个数或者false,当写入失败时返回false.
2.将格式化的csv字符串保存到字符串中,代码如下:
- $test_array = array(
- array("111","sdfsd","sdds","43344","rrrr"),
- array("sssssssss","gdfgfd","232323","wwewe","dsfds"),
- array("fgfg","e4343","dsfds","w2332","xcvxc"),
- array("11212","2323","344343","344343","rerreer"),
- array("fds","43344444","33333333","ttttttt","gggggggggggg"),
- array("kdfs","dsfdsfds","wewewe","sdsdddddddd","wwwwwwwwwww")
- );
- ob_start();
- $file = fopen("php://output","w") or die("Can't Open php://output");
- foreach($test_array as $line_array)
- {
- $isSuccess = fputcsv($file,$line_array);
- if($isSuccess===false)
- {
- die("Can't write csv line".$line_array);
- }
- }
- fclose($file) or die("Can't close file test.csv.");
- $result = ob_get_contents();
- ob_end_clean();
以用fgetcsv(file,length,separator,enclosure)函数读取csv文件.
fgetcsv的参数说明如下:
file:需要读取的csv文件,此参数是必需的。
length:表示大于csv文件中最长的行的长度的值。php5之前是必需参数。在php5中是可选参数,如果不设置此参数或者将其设为0,php将会读取.
一整行的数据。如果行的长度超过8192个字节时,应该将length值设定一个数,而不是让php自动去计算行的长度。
separator:指定数据的分隔符,默认是逗号,如果指定为“;”,那么fgetcsv函数将按照“;”来解析行数据。
fgetcsv的返回值:
根据file的一行数据,返回一个数组,如果读取文件出错,则返回false,到达文件尾部时,也返回false.
下面是一个读取test.csv文件的例子:
- $file = fopen('test.csv','r') or die("Can't open file test.csv");
- $color="#ff0000";
- print '<table border=0>';
- while($csv_line=fgetcsv($file))
- {
- print "<tr>";
- $len = count($csv_line);
- for($i=0;$i<$len;$i++)
- {
- if($i%2==0)$color="#cccccc";
- else $color="#999999";
- print '<td bgcolor='.$color.'>'.htmlentities($csv_line[$i]).'</td>';
- }
- print "</tr>";
- }
- print '</table>';
- fclose($file) or die("Can't close file test.csv!");
Tags: fputcsv() 函数 csv数据读写
- 上一篇:phpmyadmin不需要密码登录的设置方法
- 下一篇:phplot生成图片类详解
相关文章
- ·PHP中通过Web 执行C/C++应用程序(2013-11-13)
- ·用PHP实现Ftp用户的在线管理(2013-11-13)
- ·用PHP自动把纯文本转换成Web页面(2013-11-13)
- ·用实例分析PHP5异常处理(2013-11-13)
- ·php5的simplexml解析错误(2013-11-13)
- ·PHP后门的隐藏技巧测试报告(2013-11-13)
- ·PHP缓存技术详谈(2013-11-27)
- ·构建可配置PHP应用程序的正确方式(2013-11-27)
- ·利用PHP自定义错误处理器处理出错信息(2013-11-27)
- ·PHP作wap开发时遇到的问题(2013-11-27)
- ·php编写大型网站问题集(2013-11-27)
- ·php测试性能代码(2013-11-28)
- ·vsftpd+mysql的虚拟用户配置记录(2013-11-28)
- ·FastCGI 不完全高级指南(PHP版,Windows平台)(2013-11-28)
- ·php 安全register globals设置为TRUE的危害(2013-11-28)
- ·PHP require/include顺序详解(2013-11-29)
推荐文章
热门文章
最新评论文章
- 写给考虑创业的年轻程序员(10)
- PHP新手上路(一)(7)
- 惹恼程序员的十件事(5)
- PHP邮件发送例子,已测试成功(5)
- 致初学者:PHP比ASP优秀的七个理由(4)
- PHP会被淘汰吗?(4)
- PHP新手上路(四)(4)
- 如何去学习PHP?(2)
- 简单入门级php分页代码(2)
- php中邮箱email 电话等格式的验证(2)