PHP file_put_contents:在250,000行代码后创建新文件

时间:2014-05-31 12:33:09

标签: php xml seo search-engine

我使用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个网址。

我知道站点地图的上限为50,000个网址。有没有一种简单的方法在PHP中告诉脚本在一定数量的行之后创建第二个sitemap2.xml,例如。每个网址都有大约6行代码,所以我估计应该在达到250,000行代码时创建新文件?

在服务器根目录中包含多个sitemap.xml文件的最佳方法是什么,以便搜索引擎机器人能够读取所有站点地图文件?

1 个答案:

答案 0 :(得分:0)

您需要将迭代器添加到foreach。如果迭代器&gt; = 50.000

,则创建新文件