<?php
    
if (isset($_GET['source'])) {
        
highlight_file(__FILE__);
        exit;
    }

    function 
xmlerr() {
        if (
error_get_last() != NULL) {
            
error("\n\n=====\nOne of your files seems to be broken. This error is probably caused by the missing XML tree root -> Author -> Build -> Version. Try a different file maybe?\n");
        }
    }

    function 
error($msg) {
        
header('Content-Type: text/plain');
        
header_remove('Content-Disposition');
        
header_remove('Content-Length');
        die(
$msg '');
    }

    if (isset(
$_FILES['x'])) {
        
$x $_FILES['x'];

        require(
'zipstream.php');
        
$zip = new Zipstream();
        
ob_start();

        
header('Content-Type: application/zip');
        
header('Content-Disposition: attachment; filename="for-garmin.zip"');

        foreach (
$x['name'] as $id=>$name) {
            
$safename htmlspecialchars($name);

            if (
$x['size'][$id] > 1024 1024 200) {
                
error("File '$safename' larger than 200MiB. Please run the code yourself for a file this large, or contact the author to run it manually for you.");
            }

            
$f = @simplexml_load_file($x['tmp_name'][$id]);
            if (
$f === false) {
                
error("File '$safename' contains invalid XML format according to the SimpleXML Library built into PHP. Conversion could not continue.");
            }

            
register_shutdown_function('xmlerr');
            
$f->Author->Build->Version->addChild("BuildMajor"0);
            
$f->Author->Build->Version->addChild("BuildMinor"0);

            
$zip->addStream($f->asXML(), $name);
        }

        
$zip->finalize();
        exit;
    }
?>

<div style='width: 660px;'>
When importing a TCX file from Polar into Garmin, you get the error "Er is een
fout opgetreden bij het uploaden. Probeer het opnieuw." which means as much as
"An unexpected error occurred while uploading. Please try again."<br><br>

If you <strong>upload your Polar TCX file here</strong>, it will add two missing tags and give
you the fixed files in a .zip file download that <strong>should work with Garmin</strong>.<br><br>

<form method=post enctype='multipart/form-data'>
    Select files: <input name=x[] type=file multiple>
    <input type=submit value=Convert>
</form>

The technical details includes the error "An unexpected error occurred while processing the file." (500 internal server error)<br><br>

Comparing the Garmin and Polar TCX exports, I noticed that the Polar file
misses a "BuildMajor" and "BuildMinor" tag near the bottom (in
TrainingCenterDatabase - Author - Build - Version).<br><br>

In contrast to others (looking at you, tcxtools.com), the source code is publicly accessible <a href="?source">here</a>.
The zipstream.php include you see is from <a href="https://github.com/lgommans/phpzipstream">my 2016 Factorio project</a>.
</div>