PHP implementation file security download

xiaoxiao2021-03-06  47

(Mama, August 30, 2001) 13:18)

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 if the file exists

Echo "file can't find";

EXIT;

} else {

$ file = fopen ($ file_dir. $ file_name, "r"); // Open file

// Enter a file tag

Header ("Content-Type: Application / OcTet-Stream");

"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 = "info_check.exe";

$ FILE_DIR = "http://www.easycn.net/";

$ file = @ FOPEN ($ file_dir. $ file_name, "r");

IF ($ file) {

Echo "file can't find";

} else {

Header ("Content-Type: Application / OcTet-Stream");

Header ("Content-Disposition: attachment; filename =". $ File_name);

While (! Feof) {

Echo Fread ($ FILE, 50000);

}

Fclose ($ file);

}

This allows you to output a file directly with PHP.

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

New Post(0)