# What Is the 50MB Sitemap File Size Limit in PHP Sitemap Generator and How Is It Enforced?

> Understand the 50MB sitemap file size limit in PHP Sitemap Generator. Learn how msbatal/php-sitemap-generator enforces this limit during XML creation to ensure optimal performance and crawler accessibility. Discover the byte va...

- Repository: [Mehmet Selcuk Batal/php-sitemap-generator](https://github.com/msbatal/php-sitemap-generator)
- Tags: faq
- Published: 2026-03-07

---

**The PHP Sitemap Generator enforces a strict 50 MiB limit per sitemap file by validating the XML string length during the `createSitemap()` process and throwing an exception if it exceeds 52,428,800 bytes (1024 × 1024 × 50).**

The `msbatal/php-sitemap-generator` library strictly adheres to the official sitemap protocol specifications by enforcing a maximum file size on generated XML files. Understanding how the **50MB sitemap file size limit** is implemented helps you configure the generator correctly and handle potential exceptions when processing large URL sets.

## How the 50 MiB Limit Is Implemented in [`SunSitemap.php`](https://github.com/msbatal/php-sitemap-generator/blob/main/SunSitemap.php)

Inside the core [`SunSitemap.php`](https://github.com/msbatal/php-sitemap-generator/blob/main/SunSitemap.php) class, the generator validates each sitemap chunk before writing it to disk. The limit is defined as exactly **1024 × 1024 × 50 bytes** (precisely 50 MiB or approximately 52.4 MB).

During the `createSitemap()` method execution, the generator assembles the XML content for a batch of URLs and immediately checks its size:

```php
if (strlen($xml->asXML()) > 1024*1024*50) {
    throw new Exception('The size of the sitemap file is more than 50 MB. Please update your sitemap settings to reduce the number of pages.');
}

```

This check appears at **lines 208–210** of [`SunSitemap.php`](https://github.com/msbatal/php-sitemap-generator/blob/main/SunSitemap.php). If the XML string exceeds this threshold, the generator aborts the operation and throws an exception, preventing oversized files from ever being saved.

### When the Size Check Occurs

The enforcement happens **after** the XML for a specific sitemap chunk is fully assembled in memory but **before** the file is written to the filesystem. This timing ensures that no invalid files are created, preserving compliance with search engine requirements while preventing unnecessary disk I/O operations on oversized content.

## Configuring URL Counts to Avoid Size Violations

To stay under the 50MB ceiling, reduce the number of URLs per sitemap file by adjusting the `$maxUrl` constructor parameter. The generator defaults to 50,000 URLs per file, which aligns with the sitemap protocol's URL count limit, but this may still produce files exceeding 50MB if your URLs are particularly long or contain extensive metadata.

- **Primary adjustment strategy:** Lower the `$maxUrl` value when instantiating the `SunSitemap` class.
- The generator also enforces that `$maxUrl` does not exceed 50,000 entries per file as a secondary protocol compliance measure.

## Handling the Exception in Production Code

When processing large datasets, wrap the generation logic in a try-catch block to gracefully handle size limit violations:

```php
<?php
require 'SunSitemap.php';

try {
    // Initialize with reduced URL count to stay under 50MB
    $sitemap = new SunSitemap('https://example.com', 'sitemaps/', 25000, false);
    
    foreach ($largeUrlDataset as $url) {
        $sitemap->addUrl($url);
    }
    
    // Attempt generation - throws if any file exceeds 50MB
    $sitemap->createSitemap();
    echo "Sitemap generated successfully.\n";
    
} catch (Exception $e) {
    echo "Generation failed: " . $e->getMessage() . "\n";
    
    // Strategy: Reduce $maxUrl further and retry
    $sitemap = new SunSitemap('https://example.com', 'sitemaps/', 10000, false);
    // Re-add URLs and retry...
}
?>

```

This approach allows you to programmatically adjust the batch size if the initial configuration produces files that are too large.

## Compliance with Sitemap Protocol Standards

The **50MB sitemap file size limit** mirrors the official sitemap protocol specification, which mandates that individual sitemap files must not exceed 50MB (52,428,800 bytes) when uncompressed. By enforcing this at the library level, `msbatal/php-sitemap-generator` ensures that generated sitemaps will be accepted by Google, Bing, and other search engines without validation errors.

## Summary

- The generator enforces a strict **50 MiB** (52,428,800 bytes) limit per sitemap file as defined in [`SunSitemap.php`](https://github.com/msbatal/php-sitemap-generator/blob/main/SunSitemap.php) at lines 208–210.
- The size check occurs during the `createSitemap()` method, measuring `strlen($xml->asXML())` against the hardcoded threshold.
- If exceeded, the library throws an **Exception** with the message: *"The size of the sitemap file is more than 50 MB. Please update your sitemap settings to reduce the number of pages."*
- Reduce the `$maxUrl` parameter (default 50,000) to prevent violations when URLs contain lengthy query strings or extensive metadata.
- The check happens **after** XML assembly but **before** file writing to prevent invalid disk writes and ensure protocol compliance.

## Frequently Asked Questions

### What happens if my sitemap exceeds 50MB?

The generator throws an Exception immediately upon detecting the violation during the `createSitemap()` execution. The process halts before any oversized file is written to disk, ensuring your sitemap output remains compliant with search engine standards.

### How can I prevent the 50MB size limit exception?

**Lower the `$maxUrl` parameter** when instantiating the `SunSitemap` class. While the default allows 50,000 URLs, reducing this to 25,000 or 10,000 ensures each file stays under the size threshold, especially when URLs contain long paths or extensive change frequency metadata.

### Is the limit exactly 50 megabytes or 50 mebibytes?

The code uses **1024 × 1024 × 50** bytes, which equals 52,428,800 bytes or 50 mebibytes (MiB). This aligns with the sitemap protocol's specification of 50MB, interpreted in binary terms by the PHP `strlen()` function used in the validation check.

### Can I disable or increase the 50MB limit in the configuration?

No, the limit is hardcoded in [`SunSitemap.php`](https://github.com/msbatal/php-sitemap-generator/blob/main/SunSitemap.php) at line 208 as a fixed threshold. To process larger URL sets, you must use the sitemap index feature (splitting content across multiple sitemap files) rather than increasing individual file sizes, as this would violate the sitemap protocol and cause search engines to reject your submissions.