PHP implementation file security download

zhaozj2021-02-12  137

PHP implementation file security download http://xjtusky.net/Article/Article.php/488

PHP implementation file security download

You will laugh at me "download document" so simple to say? Of course, it is not as simple as it is. For example, if you want customers to fill a form, you can download a file, you must use the "redirect" method, first check if the form has been completed and complete, then refer to the file Such a customer can download, but if you want to be an e-commerce website about "online shopping", consider security issues, you don't want the user to copy the URL to download the file, I suggest you read the actual file directly using the PHP and then download it. Method is done. The procedure is as follows: $ file_name = "info_check.exe"; $ file_dir = "/ public / www / download /"; if (! File_exists ($ file_dir. $ File_name)) {// Check the file is available for an echo file Can't find "; Exit;} else {$ file = fopen ($ file_dir. $ File_name," r "); // Open file // Enter file tag header (" Content-type: Application / OcTeet-stream); Header (" Accept-ranges: Bytes "); Header (" Accept-length: ".filesize ($ file_dir. $ File_name); Header (" Content-Disposition: attachment; filename = ". $ File_name); // Output file content echo FREAD ($ File, FileSize); Fclose ($ file); exit;} If the file path is "http" or "ftp" URL, the source code will have a little change, the program is as follows: $ File_name = "info_check.exe"; $ file_dir = "www.easycn.net/"; $ file = @ FOPEN ($ file_dir. $ File_name, "r"); if (! $ File) {echo files "} Else {header (" content-type: application / octet-stream); Header ("Content-Disposition: attachment; filename ="; $ file_name); While (! Feof ($ file) {echo fread ($ FILE, 50000);} fclose ($ file);} This will directly output files with PHP.

转载请注明原文地址:https://www.9cbs.com/read-7421.html

New Post(0)