Php mandatory file download function

Source: Internet
Author: User
This article mainly introduces php mandatory file download functions, which has some reference value. interested friends can refer to the examples in this article to share with you the php mandatory file download function, for your reference, the specific content is as follows:

Public function down () {$ id = $ this-> _ get ('id'); $ M = M ("downloads "); $ data = $ M-> where ("id = $ id and status = 1")-> find ();! $ Data & exit; $ filename = iconv ('utf-8', 'gbk', $ data ['filename']); $ savename = $ data ['savename']; $ myfile = $ data [url]? $ Data [url]: 'uploads/file /'. $ savename; if (file_exists ($ myfile) {$ M-> where ("id = $ id")-> setInc ('downloads '); $ 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! ';}}

The above is all the content of this article. I hope it will help you learn and support PHP.

For more articles about force download of file functions in php, please follow the PHP Chinese network!

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.