Reading and Writing CSV Files in PHP
This article provides PHP code examples for reading data from a CSV file and writing data to a new CSV file, including handling of locale settings, skipping headers, constructing headers, and appending rows using built‑in functions such as fgetcsv, fopen, and fwrite.
Reading CSV file in PHP
function read_csv($file){
setlocale(LC_ALL,'zh_CN');//linux系统下生效
$data = null;//返回的文件数据行
if(!is_file($file)&&!file_exists($file))
{
die('文件错误');
}
$cvs_file = fopen($file,'r'); //开始读取csv文件数据
$i = 0;//记录cvs的行
while ($file_data = fgetcsv($cvs_file))
{
$i++;
if($i==1)
{
continue;//过滤表头
}
if($file_data[0]!='')
{
$data[$i] = $file_data;
}
}
fclose($cvs_file);
return $data;
}Writing CSV file in PHP
function createcsv($csv_body){
// 头部标题
$csv_header = array('sku','我们自己的成本价','京东自己的销售价','对比结果');
/**
* 开始生成
* 1. 首先将数组拆分成以逗号(注意需要英文)分割的字符串
* 2. 然后加上每行的换行符号,这里建议直接使用PHP的预定义
* 常量PHP_EOL
* 3. 最后写入文件
*/
// 打开文件资源,不存在则创建
$des_file = 'd:/res.csv';
$fp = fopen($des_file,'a');// 处理头部标题
$header = implode(',', $csv_header) . PHP_EOL;// 处理内容
$content = '';
foreach ($csv_body as $k => $v) {
$content .= implode(',', $v) . PHP_EOL;
}// 拼接
$csv = $header.$content;// 写入并关闭资源
fwrite($fp, $csv);
fclose($fp);
}These functions demonstrate basic file I/O operations for CSV handling in PHP, suitable for backend data import/export tasks.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
php Courses
php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
