PHP script to show you what's in your results directory

Hi all!
If you are like me, over the years you have accumulated quite a few results pages. In our case, we upload them to the same folder on our web server, then link to them as needed.

We find it useful to be able to go back to those files for various reasons.

I have put together a PHP file that will search a directory of Sailwave files and print out a page with the event burgee, and the title of the event, linked to the file itself. Hopefully this will help somebody!

Copy the code below into a php file (ours is called “dir.php”) and save it in the same directory on your web server as your Sailwave files. Here is an example of ours: https://www.vallartayachtclub.org/race/dir.php

It might take a few seconds to display, as I have the script checking to see if the URLs to the burgees are valid. In some cases with our older files, the original location of the burgee image changed.

I hope this helps someone!

Andy

<?php

function getEventDetails($filePath) {
    // Load the HTML content
    $htmlContent = file_get_contents($filePath);

    // Use DOMDocument to parse the HTML
    $dom = new DOMDocument();
    @$dom->loadHTML($htmlContent);

    // Find the image with alt="event burgee"
    $xpath = new DOMXPath($dom);
    $imageNode = $xpath->query('//img[@alt="event burgee"]')->item(0);
    $imageUrl = $imageNode ? $imageNode->getAttribute('src') : '';

    // Test if the image URL is valid
    if ($imageUrl) {
        $headers = @get_headers($imageUrl);
        if (!$headers || strpos($headers[0], '200') === false) {
            $imageUrl = ''; // Blank the image URL if not found or not accessible
        }
    }

    // Find the event title (assuming it's in a <title> tag or similar)
    $titleNode = $xpath->query('//title')->item(0);
    $eventTitle = $titleNode ? $titleNode->textContent : 'Unknown Event';

    // Remove "Sailwave results for" if it exists
    $eventTitle = str_replace("Sailwave results for", "", $eventTitle);
    $eventTitle = trim($eventTitle); // Trim any leading or trailing whitespace

    return [$imageUrl, $eventTitle];
}

function generateEventTable($directory) {
    // Open the directory
    $files = scandir($directory);

    // Start the table with specific styles
    echo '<table border="1" cellpadding="5" cellspacing="0" width="1%">';
    echo '<tr style="background-color: lightblue;"><th style="width: 1%; ">Event Burgee</th><th>Event Title</th></tr>';

    // Loop through each file in the directory
    foreach ($files as $file) {
        if (strpos($file, '.htm') !== false) {
            // Get event details from the file
            [$imageUrl, $eventTitle] = getEventDetails($directory . '/' . $file);

            // Add the row to the table
            echo '<tr>';
            if ($imageUrl) {
                echo '<td style="text-align: center; white-space: nowrap;"><img src="' . $imageUrl . '" alt="event burgee" style="max-height: 50px; max-width: 80px; height: auto; width: auto;"></td>';
            } else {
                echo '<td></td>'; // Blank cell if no image is found or URL is invalid
            }
            echo '<td style="width: 1%; white-space: nowrap;"><a href="' . $file . '">' . $eventTitle . '</a></td>';
            echo '</tr>';
        }
    }

    // End the table
    echo '</table>';
}

// Call the function with the current directory
generateEventTable(__DIR__);

?>