Download Counter
Download History Viewing Program
= Download and Install PHP Code =
This is an introduction to a program that allows you to know the number of downloads when a web page user downloads and uses a program. It is possible to set up a download counter on the page, and site administrators can also easily check the download history on the browser.
Note: If a page with a download link displays a message prompting file saving and is closed without initiating the download, it will still be counted as a download. This is because it counts the number of link clicks.
From this page, download the compressed "zip" file of the program and install it on your own site. The file is named "count.php", but you can change it.
You are free to use and modify the code, including changing the page design.
Please modify the code to add new functions or change the design to create a page that is easy to understand and use.
Unzip the downloaded "count.php.zip" file, and a file named "count.php" will be created. Please create a directory such as "download_history" and save it there.
The "zip" file contains only one file, "count.php," and the "login screen" for the administrator is automatically generated.
This is the main setting method. These main parts are also listed in the PHP file.
- Password Settings:
- A password must be set for the administrator page.
- The default password is "admin," but change it to any string of your choice.
- To enhance security, set a strong password.
- Setting Files to Download:
- Complete the settings for "$targetFiles = array()".
- This is the part such as "'1' => 'Your URL/file name.zip',".
- Directory for Saving Log Files:
- A directory named "log" is required on the remote server to store log files that record history.
- This program automatically creates a directory called "log" when you upload for the first time. However, if you get a message such as "No directory", please create and upload it separately.
- First-Time Upload History Display:
- When uploading for the first time, the "date" and the number "0" may be displayed in the history.
- This is because log files such as "count_1.log" containing the upload date are also generated in the automatically generated "log" directory.
- If this bothers you, download a file like "count_1.log" from the remote server, delete the data, and upload it.
- However, the "date" and "0" state may also be a history of when the counter was started. If there is a download on that day, it will be counted up.
- Administrator Viewing Page Display:
- When administrators view the page, you can choose whether to display the URL or only the file name.
- If multiple history tables are placed on one page, you can choose to keep them in the order set by "$targetFiles = array()" or sort them in the order of the date when new logs occurred.
- Page Design, CSS, etc.:
- Please adjust the page design, CSS, etc., as appropriate to make the page easy to read.
- At one time, CSS was written as an external file, but now it is written on the same page so that you can refer to the tags when changing CSS.
- Download Link Setup on the Page:
- Typically, the download <a> tag is written as follows:
<a href="/download_history/sample.zip" download="File name when downloading.zip">[Arbitrary string]</a>
- In this program, write the download <a> tag as follows:
- Match the number in "download=1" to the number set in "$targetFiles = array()". This setting refers to the files to be downloaded.
- Please match the path to your page.
- Please upload the page with the download link after uploading the file with "$targetFiles = array()" (in this example, "count.php").
[Example]Copy<a href="/download_history/count.php?download=1" download="File name when downloading.zip" target="_blank">[Arbitrary string]</a> - Typically, the download <a> tag is written as follows:
- Code for Displaying History in One Line:
- Create the following JavaScript and place it on the page you want to display:
- Match the number in "dsp_count=1" to the number set in "$targetFiles = array()".
- If you delete "&day_dsp=on," only the "total number" will be displayed without the "today/yesterday" display.
- Please match the path to your page.
[Example]Copy<script type="text/javascript" src="/download_history/count.php?dsp_count=1&day_dsp=on"></script>Total Downloads: 1865 [Today: 23 Yesterday: 76]Total Downloads: 1865
When displaying multiple tables, the program's default setting is to sort by the latest update time. If you want to sort by the total download count, create a new file like "total_downloads.php" and replace the following part.
- Change in comments
Replace the comments in the following section:// Choose whether to replace the original array with a new one sorted in the order of the new logs when displaying multiple tables on a page - Code and comment changes
Replace the code and comments in the following section. Please consider it as one block.// Get file paths and their last update dates (sort in descending order of the latest date) $filePathsAndDates = array(); foreach ($filePath as $key => $path) { if (file_exists($path)) { $filePathsAndDates[$key] = filemtime($path); } else { // Output this error to the log and decide whether to continue or interrupt the process echo "Error: File does not exist - $path<br>"; } } // Sort in descending order by the last update date (so that newer dates come first) arsort($filePathsAndDates); // Rebuild the array of sorted file paths $sortedFilePaths = array(); foreach ($filePathsAndDates as $key => $date) { $sortedFilePaths[$key] = $filePath[$key]; }
- File creation and setup
- When creating multiple pages, to prevent mistakes in setting items while adding content uniformly, create the following part of the code as an external file, and load it into the currently implemented part with a different code. Create an external file with a name like "config.php".
// Configuration of files to download. If there are multiple files, add them as '2', '3', '4', etc. Also, '0' is valid. // Use "http:" or "https:" for the protocol. $targetFiles = array( '1' => 'YourURL/FileName.zip', '2' => 'YourURL/FileName.pdf', '3' => 'YourURL/FileName.pdf', '4' => 'YourURL/FileName.pdf', );
- Code to load an external file and its placement
- Include the file created with a name like "config.php" at the following location with the code "include __DIR__ . '/config.php';".
- Comment out or delete the following section and replace it with the code "include __DIR__ . '/config.php';".
//$targetFiles = array( // '1' => 'YourURL/FileName.zip', // '2' => 'YourURL/FileName.pdf', // '3' => 'YourURL/FileName.pdf', // '4' => 'YourURL/FileName.pdf', //);
// Array that stores the total number of downloads $totalDownloads = array(); // Get the file path and its total number of downloads foreach ($filePath as $key => $path) { if (file_exists($path)) { $line = file($path); $total = 0; // Sum the number of downloads for each row foreach ($line as $val) { $valArray = explode(',', $val); $total += trim($valArray[1]); } // Store the total number of downloads in an array $totalDownloads[$key] = $total; } else { // Output this error to the log and decide whether to continue or abort the process echo "Error: File does not exist - $path"; } } // Sort by total number of downloads in descending order arsort($totalDownloads); // Rebuild the sorted array of file paths $sortedFilePaths = array(); foreach ($totalDownloads as $key => $total) { $sortedFilePaths[$key] = $filePath[$key]; }
<!doctype html> <html> <head> <meta charset="UTF-8"> <title>DownloadHistory</title> <meta name="robots" content="NOINDEX,NOFOLLOW"> </head> <body> <?php // Make only this part an external file and load it into the page you use with "include __DIR__ . '/config.php';" // Configuration of files to download. If there are multiple files, add them as '2', '3', '4', etc. Also, '0' is valid. // Use "http:" or "https:" for the protocol. $targetFiles = array( '1' => 'YourURL/FileName.zip', '2' => 'YourURL/FileName.pdf', '3' => 'YourURL/FileName.pdf', '4' => 'YourURL/FileName.pdf', ); ?> </body> </html>
include __DIR__ . '/config.php';
There may be errors in the wording as it is a translation from the Japanese version. We apologize for the inconvenience, but if there is an error in the wording in the code, please correct or adjust it yourself.