PHP implementation file security download

xiaoxiao2021-03-05  17

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 = "http://www.easycn.net/"; $ file = @ fopen ($ file_dir. $ file_name, "r"); if (! $ file) {echo "File";} 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 be directly output with PHP.

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

New Post(0)