PHP downloads attachments and updates the number of downloads

Source: Internet
Author: User
Tags ranges

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);?>

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.