Php+mysql+jquery File Download Count example explanation, _php Tutorial

Source: Internet
Author: User
Tags jquery library

Php+mysql+jquery File Download Count example explained,


In the project we need to download the number of statistics files, the user every time the file download, the corresponding number of downloads plus 1, similar applications in many download stations. In this paper, the use of php+mysql+jquery, the implementation of the click File, download files, the number of cumulative process, the whole process is very smooth.

Preparatory work
This example requires the reader to have PHP, Mysql, jquery and HTML, CSS and other related basic knowledge, before developing the sample, you need to prepare Mysql data table, this article assumes that there is a file download table downloads, used to record file names, The file name and number of downloads that were saved on the document server. The assumption is that the data already exists in the download table, which may be inserted from the background upload file in the project so that we can read it in 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 (#) not NULL,  ' savename ' varchar (NOT null),  ' downloads ' int (ten) unsigned NOT null DEFAULT ' 1 ',  PRIMARY KEY (' id '),  

You can also download the demo directly, import the SQL file, the data are available.
Html
We add the following HTML structure to the Index.html page body, where ul.filelist is used to display the list of files, and now there is no content in it, we will use jquery to read the file list asynchronously, so don't forget, we also need to load the jquery library file in HTML.

   
 
  
  

Css
In order to let the demo better display the page effect, we use CSS to decorate the page, the following code mainly sets the file list display effect, of course, the actual project can be set according to the appropriate style.

 #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;p adding:5px;position:absolute; margin-left:10px;text-decoration:none;} 

Php
For a better understanding, we divide two PHP files, One is filelist.php, which reads data from the MySQL data table and outputs the data in JSON format to the foreground index.html page call, and the other is download.php, which responds to the download action, updates the number of downloads for the corresponding file, and completes the download through the browser. Filelist.php reads the downloads table and outputs the data in JSON format via Json_encode (), which is prepared for the following Ajax asynchronous operation.

Require ' conn.php '; Connect 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 ']     );   }   

download.php according to the URL parameter, the query to get the corresponding data, detect the existence of the file to be downloaded, if present, update the corresponding data download number +1, and use header () to implement the download function. It is worth mentioning that the header () function is used to force the download of the file, and you can set the file name to be saved to local after download. In general, we use the background upload program will be uploaded files renamed and saved to the server, a common date-time named file, one of the benefits of this is to avoid the file name duplication and Chinese name garbled situation. The files we download to local can use the header ("content-disposition:attachment; Filename= ". $filename) Set the file name to an easy-to-recognize filename.

Require (' conn.php ');//Connect database $id = (int) $_get[' id '];  if (!isset ($id) | | $id ==0) die (' parameter wrong! '); $query = mysql_query ("Select * from Downloads where id= ' $id '"); $row = Mysql_fetch_array ($query); if (! $row) exit; $filename = Iconv (' UTF-8 ', ' GBK ', $row [' filename ']);//Chinese name note the conversion code $savename = $row [' Savename ']; The actual saved name on the server $myfile = ' file/'. $savename; if (file_exists ($myfile)) {//If file exists   //update download   mysql_query ("Update downloads set downloads=downloads+1 where ID = ' $id ');   Download 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{   

Jquery
Front page jquery mainly completes two tasks, one is to read the list of files asynchronously through Ajax and show, second, in response to user click events, the corresponding file download number +1, to see the code:

$ (function () {   $.ajax ({///asynchronous Request     Type: ' GET ',     URL: ' filelist.php ',     dataType: ' json ',     Cache:false,     beforesend:function () {       $ (". FileList"). HTML ("
  • Loading in ...
  • "); }, 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 a '). Live (' click ', Function () { var count = $ ('. Downcount ', this); Count.text (parseint (Count.text ()) +1); Download Count +1

    First, after the page is loaded, send a Get-form Ajax request through $.ajax () to the background filelist.php, and when Filelist.php succeeds, receives the returned JSON data, traversing the JSON data object through $.each (), Constructs an HTML string and adds the resulting string to the ul.filelist, creating a list of files in the demo.
    Then, when you click on the file download, the download count is incremented by using live () to respond to the click event of a dynamically joined list element.
    Finally, actually read through this article, this is a we usually apply to the Ajax case, and of course, PHP with MySQL implementation of the download knowledge, I hope to be helpful.

    http://www.bkjia.com/PHPjc/1060095.html www.bkjia.com true http://www.bkjia.com/PHPjc/1060095.html techarticle php+mysql+jquery File Download count example, the project we need to download the number of statistics files, the user every time the download file, the corresponding download times plus 1, similar applications in ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.