php 用header()读写csv文件方法
定义和用法
header() 函数向客户端发送原始的 HTTP 报头。
认识到一点很重要,即必须在任何实际的输出被发送之前调用 header() 函数(在 PHP 4 以及更高的版本中,您可以使用输出缓存来解决此问题):
// 结果出错
// 在调用 header() 之前已存在输出
header('Location: http://www.zhutiai.com/');
?>语法
header(string,replace,http_response_code)参数 描述
string 必需。规定要发送的报头字符串。
replace 可选。指示该报头是否替换之前的报头,或添加第二个报头。
默认是 true(替换)。false(允许相同类型的多个报头)。
http_response_code 可选。把 HTTP 响应代码强制为指定的值。(PHP 4 以及更高版本可用)
1.定义header()头部输出格式
header("Content-type:application/vnd.ms-excel"); //定义输出的文件类型
header(“content-Disposition:filename=downloaded.pdf”); //定义输出的文件名,也就是设置一个下载类型,下载的时候对文件从新命名
header("Content-type:application/vnd.ms-excel");
header("content-Disposition:filename=downloaded.pdf ");echo"1t 2t 3n"; //其中 t 是空白 ,n是回车 (编码规范 不能直接输出)
echo"1t 2t 3n";
echo"1t 2t 3n";
?>
这时候就可以打开这个php文件,就会提示下载。
也可以以table表格的方式输出;
header("Content-type:application/vnd.ms-excel ");
header("content-Disposition:filename=downloaded.pdf");
?>
t00 t01 t02
t10 t11 t12
t20 t21 t22
CSV的写入操作:
fputcsv()的使用可参考.
$fp = fopen('f:/file.csv', 'w');
fputcsv($fp,array('aaa','bbb','cccc'));
fputcsv($fp,array('mmm','yyy','haha')); //fputcsv()可以用数组循环的方式进行实现
fclose($fp);

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Zend Studio 13.0.1
Powerful PHP integrated development environment

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),
