I have a download section in my index.php that I'd like to add a download counter to but have no idea how to accomplish this. The existing code works perfectly but I'd like to know how many times the file is downloaded. Would prefer not to use a database.

<!--  DOWNLOAD -->
<div id="download" class="stylized">
    <div "myform">
    <div class="container">
        <div class="row">
            <div class="download">
                <br /><br>
                <h1><center>FoxClone Download Page</center></h1>
                   <?php
                    $files = glob('download/*.iso');
                    $file = $files[count($files) -1];
                    $info = pathinfo($file);
                    $filename =  basename($file);
                    $filename = ltrim($filename,'/');
                    $md5file = md5_file($file);
                   ?>

                     <div class="container">
                        <div class="divL">                       
                            <h3>Get the "<?php echo "{$filename}";?>" file (approx. 600MB)</h3>
                        <center>  <a href="<?php echo "/{$file}";?>"><img src="images/button_get-the-app.png" alt=""></a> </center><br />
                            <h3 style="margin-bottom: 0.5rem;">The MD5sum for "<?php echo "{$filename}";?>" is "<?php echo "{$md5file}";?>  

Thanks in advance,
Larry

Recommended Answers

All 3 Replies

Your code <a href="<?php echo "/{$file}";?>"> is a link to the actual .iso file you want them to download.

Instead, you want to create a new page such as download.php?file=123 where an ID for the file is passed in as a parameter. When that page is accessed, it increments a counter in a database for file 123, and then it redirects the browser to the location of the file.

Do you already have a database set up for this web app?

No I don't, Dani. If fact, I've never worked on a website that has a database. The main thing is that I don't want the user to leave the download page.

I just set-up a database with 2 tables, sql follows:

CREATE TABLE DOWN (
id INT NOT NULL AUTO_INCREMENT,
session_id INT NOT NULL,
filename VARCHAR NOT NULL,
when_down DATETIME,
ip_address VARCHAR NOT NULL,
PRIMARY KEY (id)
);

CREATE TABLE LOGIN (
id INT NOT NULL AUTO_INCREMENT,
session_id INT NOT NULL,
when_login DATETIME,
ip_address CHAR NOT NULL,
PRIMARY KEY (id)
);
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.