This article mainly describes examples of statistics on the number of PHP + Mysql + jQuery file downloads.
This article mainly describes examples of statistics on the number of PHP + Mysql + jQuery file downloads.
In the project, we need to count the number of file downloads. Each time a user downloads a file, the corresponding number of downloads increases by 1. Similar applications are used in many download sites. This article uses PHP + Mysql + jQuery in combination with examples to achieve the process of clicking files, downloading files, and accumulating the number of times. The whole process is very smooth.
Preparations
In this example, you need to have basic knowledge about PHP, Mysql, jQuery, html, css, and so on. Before developing an example, you need to prepare a Mysql DATA table. In this example, assume that there is a downloads File Download table, used to record the file name, the file name stored on the file server, and the number of downloads. The premise is that if data already exists in the download table, the data may be inserted when the file is uploaded in the background of the project, so that we can read the data on the page. The downloads table structure is as follows:
Create table if not exists 'downloads' ('id' int (6) unsigned not null AUTO_INCREMENT, 'filename' varchar (50) not null, 'savename' varchar (50) not null, 'downloads' int (10) unsigned not null default '1', primary key ('id'), unique key 'filename' ('filename ')) ENGINE = MyISAM default charset = utf8;
You can also directly download the Demo and import the SQL file. All the data is available.
HTML
We add the following HTML structure to the body of the index.html page, where ul. filelist is used to display the file list. Now there is no content in it. We will use jQuery to asynchronously read the file list, so don't forget, we also need to load the jQuery library file in html.
CSS
To make the demo better display the page effect, we use CSS to modify the page. The following code mainly sets the Display Effect of the file list. Of course, you can set the corresponding style as needed in the actual project.
# Demo {width: 728px; margin: 50px auto; padding: 10px; border: 1px solid # ddd; background-color: # eee;} ul. filelist li {background: url ("img/bg_gradient.gif") repeat-x center bottom # F5F5F5; border: 1px solid # ddd; border-top-color: # fff; list-style: none; position: relative;} ul. filelist li. load {background: url ("img/ajax_load.gif") no-repeat; padding-left: 20px; border: none; position: relative; left: 150px; top: 30px; width: 200px} ul. filelist li a {display: block; padding: 8px;} ul. filelist li a: hover. download {display: block;} span. download {background-color: #64b126; border: 1px solid #4e9416; color: white; display: none; font-size: 12px; padding: 2px 4px; position: absolute; right: 8px; text-decoration: none; text-shadow: 0 0 1px # 315d0d; top: 6px;-moz-border-radius: 3px;-webkit-border-radius: 3px; border-radius: 3px;} span. downcount {color: #999; padding: 5px; position: absolute; margin-left: 10px; text-decoration: none ;}
PHP
The others page is called, and the other is download. php, which is used to respond to the download action, update the download times of the corresponding file, and complete the download through the browser. Filelist. php reads the downloads table and Outputs Data in JSON format through json_encode (). This is prepared for the following Ajax asynchronous operations.
Require 'conn. php '; // connect to the database $ result = mysql_query ("SELECT * FROM downloads"); if (mysql_num_rows ($ result) {while ($ row = mysql_fetch_assoc ($ result )) {$ data [] = array ('id' => $ row ['id'], 'file' => $ row ['filename'], 'downloads' => $ row ['downloads']);} echo json_encode ($ data );}
Download. php transmits Parameters Based on the url to query the corresponding data and check whether the file to be downloaded exists. If so, the number of downloads of the corresponding data is updated + 1 and the header () is used () download. It is worth mentioning that the header () function is used to forcibly download an object, and you can set the name of the object to be downloaded and saved locally. In general, we use the background upload program to rename the uploaded files and save them to the server. Commonly, There are files named by date and time, one of these benefits is to avoid repeated file names and garbled Chinese names. To download a local file, you can use header ("Content-Disposition: attachment; filename =". $ filename) to set the file name to a name that is easy to recognize.
Require ('conn. php'); // connect to the database $ id = (int) $ _ GET ['id']; if (! Isset ($ id) | $ id = 0) die ('parameter error! '); $ Query = mysql_query ("select * from downloads where"); $ row = mysql_fetch_array ($ query); if (! $ Row) exit; $ filename = iconv ('utf-8', 'gbk', $ row ['filename']); // note the conversion encoding for the Chinese name $ savename = $ row ['savename']; // The actual storage name on the server $ myfile = 'file /'. $ savename; if (file_exists ($ myfile) {// if the file exists // The number of update downloads mysql_query ("update downloads set downloads = downloads + 1 where "); // download the file $ file = @ fopen ($ myfile, "r"); header ("Content-type: application/octet-stream"); header ("Content-Disposition: attachment; filename = ". $ filename ); While (! Feof ($ file) {echo fread ($ file, 50000) ;}fclose ($ file); exit;} else {echo 'the file does not exist! ';}
JQuery
JQuery on the front-end page mainly completes two tasks: Reading the file list asynchronously through Ajax and displaying the file list; responding to the user Click Event, adding the corresponding file download times to 1. Let's see the code:
$ (Function () {$. ajax ({// asynchronous request type: 'get', url: 'filelist. php ', dataType: 'json', cache: false, beforeSend: function () {$ (". filelist "pai.html ("
Loading...") ;}, Success: function (json) {if (json) {var li =''; $. each (json, function (index, array) {li = li +'
'+ Array ['file'] + ''+ array ['downloads'] +' click to download';}); $ (". Filelist ").html (li) ;}}); $ ('ul. filelist '). live ('click', function () {var count = $ ('. downcount', this); count. text (parseInt (count. text () + 1); // number of downloads + 1 });});