This Code does not know why. The actual number of downloads during execution is not + 1 but + 2.
There is an improvement below this code, and the number of downloads is only 1.
<? Phprequire_once ('. /login. PHP '); downloadattachment (); // download the attachment function downloadattachment () {Global $ conn; // obtain the ID $ id = $ _ request ['file _ id']; // query the database $ attachurl = ""; $ attachsql = "select Ming, URL, downloadnum from articleattach where id = '$ id '"; $ result = $ Conn-> execute ($ attachsql); if ($ result) {$ attachurl = $ result-> fields ['url']; $ attachming = $ result-> fields ['ming']; $ downloadnum = $ result-> fields ['down Loadnum'] + 1; // download the attachment downloads ($ attachming, $ attachurl, $ downloadnum, $ id) ;}} function downloads ($ name, $ Dir, $ downloadnum, $ id) {Global $ conn; If (! File_exists ($ DIR) {// the file header ("Content-Type: text/html; charset = UTF-8"); echo "file not found! "; Exit;} else {// download file $ file = fopen ($ Dir," R "); header (" Content-Type: Application/octet-stream "); header ("Accept-ranges: bytes"); header ("Accept-length :". filesize ($ DIR); header ("content-Disposition: attachment; filename = ". $ name); echo fread ($ file, filesize ($ DIR); fclose ($ file ); // update database // echo "Update articleattach set downloadnum = '$ downloadnum' where id =' $ id'"; $ attachsql = "Update articleattach set do Wnloadnum = '$ downloadnum' where id =' $ id' "; $ Conn-> execute ($ attachsql) ;}}?>
After improvement:
Attachdownload. php
<? Phprequire_once ('. /login. PHP '); header ("Content-Type: text/html; charset = UTF-8"); downloadattachment (); // download the attachment function downloadattachment () {Global $ conn; // obtain ID $ id = $ _ request ['file _ id']; // query the database $ attachurl = ""; $ attachsql = "select Ming, URL, downloadnum from articleattach where id = '$ id' "; $ result = $ Conn-> execute ($ attachsql); if ($ result) {$ attachurl = $ result-> fields ['url']; $ attachming = $ result-> fi ELDS ['ming']; $ downloadnum = $ result-> fields ['downloadnum'] + 1; // download the attachment downloads ($ attachming, $ attachurl, $ downloadnum, $ id) ;}} function downloads ($ name, $ Dir, $ downloadnum, $ id) {Global $ conn; If (! File_exists ($ DIR) {// The file echo "file not found is not found! "; Exit;} else {// update database $ attachsql =" Update articleattach set downloadnum = '$ downloadnum' where id =' $ id '"; $ Conn-> execute ($ attachsql); // jump to the header ("Location: downloadfile. PHP? Name = $ name & dir = $ dir ") ;}}?>
Downloadfile. php
<? Phprequire_once ('. /login. PHP '); $ dir = $ _ request ['dir']; $ name = $ _ request ['name']; // download file $ file = fopen ($ Dir, "R"); header ("Content-Type: Application/octet-stream"); header ("Accept-ranges: bytes"); header ("Accept-length :". filesize ($ DIR); header ("content-Disposition: attachment; filename = ". $ name); echo fread ($ file, filesize ($ DIR); fclose ($ file);?>