PHP implements the following secure file download program: 1234567891011121314151620.file_name="=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.