What potential issues could arise from counting open and closed connections in a PHP script for tracking downloads?

One potential issue that could arise from counting open and closed connections in a PHP script for tracking downloads is inaccurate tracking if connections are not properly closed. To solve this issue, ensure that connections are explicitly closed after each download is complete to accurately count the number of downloads.

// Open connection to file for download
$file = 'example.pdf';
$handle = fopen($file, 'rb');

// Output file contents to browser
header("Content-Type: application/pdf");
header("Content-Disposition: attachment; filename=" . basename($file));
fpassthru($handle);

// Close connection after download is complete
fclose($handle);