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

Dani AI

Generated

was on the right track: route downloads through a small endpoint (for example download.php?id=123) that increments your counter and then serves or redirects to the file. That gives you an authoritative place to log/count without exposing raw filesystem paths, and it also lets you start the download without navigating the user away from the page (open the endpoint in a new tab, a hidden iframe, or trigger it with JavaScript).

If you only need simple totals, use one small table keyed by filename; if you want per-download audit info keep a separate log table. Example schemas (short, corrected versions — note VARCHAR needs a length and ip_address should allow IPv6):

CREATE TABLE files (
  id INT AUTO_INCREMENT PRIMARY KEY,
  filename VARCHAR(255) NOT NULL UNIQUE,
  downloads INT UNSIGNED NOT NULL DEFAULT 0,
  last_download DATETIME NULL,
  last_ip VARCHAR(45) NULL
);

CREATE TABLE download_log (
  id INT AUTO_INCREMENT PRIMARY KEY,
  file_id INT NOT NULL,
  session_id VARCHAR(128),
  ip_address VARCHAR(45),
  when_down DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  INDEX(file_id),
  FOREIGN KEY (file_id) REFERENCES files(id)
);

Minimal flow for download.php: validate the incoming id against your files table (whitelist), run a single prepared UPDATE to increment downloads (and optionally INSERT into download_log), fetch the filename, then either redirect to /download/<file> or instruct the webserver to serve the file (X-Sendfile / X-Accel-Redirect) instead of streaming it through PHP for large ISOs. Example outline (PDO + prepared statements) keeps things safe and avoids SQL injection.

Cautions and tips: never accept raw filenames from users (map IDs to real paths), use basename()/whitelists to prevent traversal, prefer X-Sendfile or nginx accel for large files, and dedupe counts if you want to avoid bot/refresh inflation (e.g., one count per session/IP per X minutes). Also, the CREATE TABLE you posted will fail because VARCHAR/CHAR require lengths; use the corrected patterns above.

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.