What are the best practices for implementing a download counter in PHP without using a database?

When implementing a download counter in PHP without using a database, one approach is to store the download count in a text file. Each time a download occurs, the count is incremented and saved back to the file. This allows for tracking the number of downloads without the need for a database.

<?php
$counter_file = 'download_count.txt';

// Check if the counter file exists, if not create it with initial count of 0
if (!file_exists($counter_file)) {
    file_put_contents($counter_file, '0');
}

// Read the current count from the file
$download_count = file_get_contents($counter_file);

// Increment the count
$download_count++;

// Save the updated count back to the file
file_put_contents($counter_file, $download_count);

// Output the download count
echo 'Total Downloads: ' . $download_count;
?>