PHP example Tutorial: PHP program for secure file download

Source: Internet
Author: User
Tags php example
PHP implements the following secure file download program: 1234567891011121314151620.file_name=&quot=info_check.exe & quot; $ file_dir = & quot;/public/www/download/& quot; if (! File_exists ($ file_dir. $ file_nam

 

Secure file download using PHP

The procedure is as follows:

 
12345678910111213141516 $ File_name = "info_check.exe"; $ file_dir = "/public/www/download/"; if (! File_exists ($ file_dir. $ file_name) {// check whether the file exists. echo "the file cannot be found"; exit;} else {$ file = fopen ($ file_dir. $ file_name, "r"); // open a file // enter the file tag Header ("Content-type: application/octet-stream"); Header ("Accept-Ranges: bytes "); Header (" Accept-Length :". filesize ($ file_dir. $ file_name); Header ("Content-Disposition: attachment; filename = ". $ file_name); // echo fread ($ file, filesize ($ file_dir. $ file_name); fclose ($ file); exit ;}

If the file path is "http" or "ftp", the source code will change a little. The program is as follows:

 
12345678910111213 $ File_name = "info_check.exe"; $ file_dir = "www.dwww.cn/"; $ file = @ fopen ($ file_dir. $ file_name, "r"); if (! $ File) {echo "file not found";} else {Header ("Content-type: application/octet-stream"); Header ("Content-Disposition: attachment; filename = ". $ file_name); while (! Feof ($ file) {echo fread ($ file, 50000) ;}fclose ($ file );}

In this way, you can use PHP to directly output files.

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.