Php 处理内存限制
我有将数据导出到csv文件的功能:Php 处理内存限制,php,mysql,csv,Php,Mysql,Csv,我有将数据导出到csv文件的功能: $products = //databse query foreach($products as $key => $product) { $products[$key]['example'] = 'example'; . . . } $csv = new parseCSV(); $csv->output ('products.cs
$products = //databse query
foreach($products as $key => $product) {
$products[$key]['example'] = 'example';
.
.
.
}
$csv = new parseCSV();
$csv->output ('products.csv', $products, $labels, $delimiter);
问题在于,当用户保存许多记录时,内存限制。
如何在不增加内存限制的情况下处理它?您应该逐行编写csv文件,请看下面的示例:
您应该逐行编写csv文件,请看下面的示例: 找到对我有意义的
<?php
$handle = fopen('somefile.csv', 'r');
if ($handle)
{
set_time_limit(0);
//the top line is the field names
$fields = fgetcsv($handle, 4096, ',');
//loop through one row at a time
while (($data = fgetcsv($handle, 4096, ',')) !== FALSE)
{
$data = array_combine($fields, $data);
}
fclose($handle);
}
?>
找到对我有意义的
<?php
$handle = fopen('somefile.csv', 'r');
if ($handle)
{
set_time_limit(0);
//the top line is the field names
$fields = fgetcsv($handle, 4096, ',');
//loop through one row at a time
while (($data = fgetcsv($handle, 4096, ',')) !== FALSE)
{
$data = array_combine($fields, $data);
}
fclose($handle);
}
?>
将列表分块,并使用for循环将其命名为“products\u x.csv”
$start = isset($_GET['start']) ? $_GET['start'] : 0;
for ( $i=$start; $i < $start + $MAX; $i++) {
// add products to csv
}
// csv output and download
$start=isset($\u GET['start'])$_获取['start']:0;
对于($i=$start;$i<$start+$MAX;$i++){
//将产品添加到csv
}
//csv输出和下载
将列表分块,并使用for循环将其命名为“products\u x.csv”
$start = isset($_GET['start']) ? $_GET['start'] : 0;
for ( $i=$start; $i < $start + $MAX; $i++) {
// add products to csv
}
// csv output and download
$start=isset($\u GET['start'])$_获取['start']:0;
对于($i=$start;$i<$start+$MAX;$i++){
//将产品添加到csv
}
//csv输出和下载
哦,等等,你在寻找一种写CSV而不是从中读取的方法?哦,等等,你在寻找一种写CSV而不是从中读取的方法?最好是创建文件,然后添加数据或将所有内容都输出?或者这无关紧要?创建文件然后添加数据或将所有内容都输出是更好的做法吗?或者这不重要?