**

How To 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>

I need a clear example of this process. Thanks.

AndreRet commented: Welcome! Please post the counter function code as well, thanks. +0

AndreRet
This is the php function for the download

<?php
if(isset($_REQUEST["file"])){
    // Get parameters
    $file = urldecode($_REQUEST["file"]); // Decode URL-encoded string

    /* Test whether the file name contains illegal characters
    such as "../" using the regular expression */
    if(preg_match('/^[^.][-a-z0-9_.]+[a-z]$/i', $file)){
        $filepath = "images/" . $file;

        // Process download
        if(file_exists($filepath)) {
            header('Content-Description: File Transfer');
            header('Content-Type: application/octet-stream');
            header('Content-Disposition: attachment; filename="'.basename($filepath).'"');
            header('Expires: 0');
            header('Cache-Control: must-revalidate');
            header('Pragma: public');
            header('Content-Length: ' . filesize($filepath));
            flush(); // Flush system output buffer
            readfile($filepath);
            die();
        } else {
            http_response_code(404);
            die();
        }
    } else {
        die("Invalid file name!");
    }
}
?>
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.