PHP implementation file security download

zhaozj2021-02-11  169

You may laugh at me "Download Document" is 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 = "tianhys.exee"; $ file_dir = "/ public / www / download /"; if (! file_exists ($ file_dir. $ file_name))) {// Check if the file has an echo "file Can't find"; exit } Else {$ file = fopen ($ file_dir. $ File_name, "r"); // Open file // Enter 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); // Output file content echo fread ($ File, FileSize ($ file_dir. $ file_name); 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 = "tianhys.exe"; $ file_dir = "http://www.tianhys.org/"; $ 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 allows you to output a file directly with PHP.

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

New Post(0)