Oj Obasi
Oj Obasi

I am also and will continue to be a student of web design and development. My tutorials may not be for you. If you feel that way, then search online for better tutorials.

Set link (anchor tag) click to download a file and count downloads using php

When the link is clicked, a file named myfile.rar will be downloaded from the server.We will also be counting the number of downloads. Create a webpage name index.php add the php and html code below.

Also create the myfile.rar you want to be downloaded .

And finally create a text file call counter.txt

Upload all files to server and try it out.

Php code

<?php
if(!file_exists('counter.txt')){
file_put_contents('counter.txt', '0');
}
$click = "";
if(isset($_GET['click']) == 'yes'){
file_put_contents('counter.txt', ((int) file_get_contents('counter.txt')) + 1);
header("location:myfile.rar");exit();
}
?>

Html code

<div><a href="index.php?click=yes">Download It Now</a></div>
<div>Downloads:<em><?php echo file_get_contents('counter.txt'); ?></em></div>