PHP file_put_contents : Create new file after 250, 000行代码

标签 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,例如。每个 url 将有大约 6 行代码,所以我估计新文件应该在达到 250,000 行代码时创建?

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

最佳答案

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

则创建新文件

关于PHP file_put_contents : Create new file after 250, 000行代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23970129/

相关文章:

php - 如何从 PHP 开始一个新的 HTML 行?

php - Gearman 的工作和任务有什么区别?

java - 如何用 Java 浏览和显示 XML 内容

javascript - 修复 AngularJS 网站的#tag inurl?

web - 旧网站(2002)到新网站(2014)

HTML 元数据 "keywords"。值得包括吗​​?

php - 如何对 sql 表中的不同值执行摘要

php - 如何将 mysql_real_escape_string 设为零

javascript - 如何使用 js 和 php 获取当前(窗口)屏幕尺寸

php - 将 PHP 设置传递给 Javascript 的最佳方式是什么?