我有一个PHP脚本,可以将数据写入5000个表行的批量文件中。当所有行都写入文件时,它应该输出运行脚本所花费的时间。相反,正在发生的事情是脚本似乎在浏览器上不断运行,输出永远不会出现,但文件存在,这意味着脚本已经运行。这只发生在大量数据上。有什么建议吗?
$startTime = time();
$ID = '123';
$productBatchLimit = 5000;
$products = new Products();
$countProds = $products->countShopKeeperProducts();
//limit the amount of products
//if ($countProds > $productBatchLimit){$countProds = $productBatchLimit; }
$counter = 1;
for ($i = 0; $i < $countProds; $i += $productBatchLimit) {
$xml_file = 'xml/products/'. $ID . '_'. $counter .'.xml';
$xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
$xml .= "<products>\n";
//create new file
$fh = fopen($xml_file, 'a');
fwrite($fh, $xml);
$limit = $productBatchLimit*$counter;
$prodList = $products->getProducts($i, $limit);
foreach ($prodList as $prod ){
$xml = "<product>\n";
foreach ($prod as $key => $value){
$value = functions::xml_entities($value);
$xml .= "<{$key}>{$value}</{$key}>\n";
}
$xml .= "</product>\n";
fwrite($fh, $xml);
}
$counter++;
fwrite($fh, '</products>');
fclose($fh);
}
//check to see when XML is fully formed
$validxml = XMLReader::open($xml_file);
$validxml->setParserProperty(XMLReader::VALIDATE, true);
if ($validxml->isValid()==true){
$endTime = time();
echo "Total time to generate results: ".($endTime - $startTime)." seconds. \n";
} else {
echo "Problem saving Products XML.\n";
}