I have a webpage which helps download files from database. I want to count the how many times the download button has been clicked and display it on my webpage with unique id. This is my download webpage

<div class="container">
<div class="row">
<div class="col-12">
<div class="media">
<img src="./images/<?= $mainImage; ?>" alt="PowerBeatz" width="300" height="300" />
<div class="media-body">
<h5 class="mt-0"><span class="title"><?php echo $row['song']; ?></span></h5>
<?php echo $row['artist']; ?>
<div class="play">
<a href="file.php?id=<?php echo $row['song_id']; ?>" class="btn btn-primary"><i class="fa fa-download"></i> </a>
</div>
<div>
<p>Total Downloads: </p>
</div>
</div>
</div>
</div>
</div>
</div>

You can create a PHP function to handle the download requests, and increment the counter inside the function, for example, create download.php and pass the requested file as a GET parameter:

file_counter_inc(); // <-- The increment happens here

$file_url = $_GET['file_url'];
header('Content-Type: application/octet-stream');
header("Content-Transfer-Encoding: Binary");
header("Content-disposition: attachment; filename=\"MyFile\"");
readfile($file_url);
Then you output the download link like this:

<a href="download.php?file_url=downloads/<?php echo $website->temp_link ?>"> Download </a>

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.