There are often import/export requirements at work. The following are commonly used methods.
To read a CSV file, you can read it in pages, just set the number of lines to read and the number of starting lines.
Export CSV files, implemented in two ways.
/**<br>
* Read CSV file<br>
* @param string $csv_file csv file path<br>
* @param int $lines Read the number of lines<br>
* @param int $offset Starting line number<br>
* @return array|bool<br>
*/<br>
public function read_csv_lines($csv_file = '', $lines = 0, $offset = 0)<br>
{<br>
if (!$fp = fopen($csv_file, 'r')) {<br>
return false;<br>
}<br>
$i = $j = 0;<br>
while (false !== ($line = fgets($fp))) {<br>
if ($i++
continue;<br>
}<br>
break;<br>
}<br>
$data = array();<br>
while (($j++
$data[] = fgetcsv($fp);<br>
}<br>
fclose($fp);<br>
return $data;<br>
}<br>
<br>
<br>
/**<br>
* Export CSV file<br>
* @param array $data Data<br>
* @param array $header_data first row of data<br>
* @param string $file_name File name<br>
* @return string<br>
*/<br>
public function export_csv_1($data = [], $header_data = [], $file_name = '')<br>
{<br>
header('Content-Type: application/octet-stream');<br>
header('Content-Disposition: attachment; filename=' . $file_name);<br>
if (!empty($header_data)) {<br>
echo iconv('utf-8','gbk//TRANSLIT','"'.implode('","',$header_data).'"'."n");<br>
}<br>
foreach ($data as $key => $value) {<br>
$output = array();<br>
$output[] = $value['id'];<br>
$output[] = $value['name'];<br>
echo iconv('utf-8','gbk//TRANSLIT','"'.implode('","', $output).""n");<br>
}<br>
}<br>
<br>
/**<br>
* Export CSV file<br>
* @param array $data Data<br>
* @param array $header_data first row of data<br>
* @param string $file_name File name<br>
* @return string<br>
*/<br>
public function export_csv_2($data = [], $header_data = [], $file_name = '')<br>
{<br>
header('Content-Type: application/vnd.ms-excel');<br>
header('Content-Disposition: attachment;filename='.$file_name);<br>
header('Cache-Control: max-age=0');<br>
$fp = fopen('php://output', 'a');<br>
if (!empty($header_data)) {<br>
foreach ($header_data as $key => $value) {<br>
$header_data[$key] = iconv('utf-8', 'gbk', $value);<br>
}<br>
fputcsv($fp, $header_data);<br>
}<br>
$num = 0;<br>
//每隔$limit行,刷新一下输出buffer,不要太大,也不要太小<br>$limit = 100000;<br>
//Get data line by line without wasting memory<br>
$count = count($data);<br>
If ($count > 0) {<br>
for ($i = 0; $i
$num++;<br>
//Refresh the output buffer to prevent problems caused by too much data<br>
if ($limit == $num) {<br>
ob_flush();<br>
flush();<br>
$num = 0;<br>
}<br>
$row = $data[$i];<br>
foreach ($row as $key => $value) {<br>
$row[$key] = iconv('utf-8', 'gbk', $value);<br>
}<br>
fputcsv($fp, $row);<br>
}<br>
}<br>
fclose($fp);<br>
}
For more [dry information sharing], please follow the PHP engineer subscription account.

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

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

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.

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft