PHP file_put_contents:在25万行代码之后创建新文件


PHP file_put_contents: Create new file after 250,000 lines of code

我正在使用php脚本生成我的网站sitemap.xml

$conn = new PDO("mysql:host=$hostdb; dbname=$namedb", $userdb, $passdb);
  $conn->exec("SET CHARACTER SET utf8");      // Sets encoding UTF-8
  // Define and perform the SQL SELECT query
  $sql = "SELECT `title`,`id`,`type`,`updated_at` FROM `titles`";
  $result = $conn->query($sql);
  // If the SQL query is succesfully performed ($result not false)
  if($result !== false) {
    // Parse the result set, and add the URL in the XML structure
    foreach($result as $row) {
      $xmlsitemap .=  '<url>
<loc>/'. $type1 .'/'. $row['id'] .'-'. $string .'</loc>
<lastmod>'. $date1 .'</lastmod>
<priority>0.5</priority>
<changefreq>weekly</changefreq>
</url>';
    }
  }
$xmlsitemap .= '</urlset>';
file_put_contents($xmlfile, $xmlsitemap);          // saves the sitemap on server

目前我的sitemap.xml文件中大约有37,000个url。

我知道站点地图有50,000个url的上限。在php中是否有一种简单的方法来告诉脚本创建第二个sitemap2.xml一旦达到一定的行数。每个url都将有大约6行代码,所以我估计新文件应该在达到25万行代码时创建?

还有什么是最好的方式,包括多个sitemap.xml文件在您的服务器根,使所有的sitemap文件将被搜索引擎机器人读取?

需要为foreach添加迭代器。并创建新文件if iterator>=50.000