PHP implementation file download

xiaoxiao2021-03-06  61

You will laugh at me "download document" so simple to say? Of course, it is not as simple as you think. 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 In this way, customers can download, such as the following code written by the author:

/ / Check if Form is completed ...

IF ($ form_completed) {

HEADER ("Location:

http://www.myweb.com/download/info_check.exe "

;

EXIT;

}

?>

Or the following cases:

" Start downloading files

This uses the ID method to receive the number to download the file, and then connect to the actual URL with "redirect".

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 actual file and download it. 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 a file

// Enter a 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 = "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);

}

?>

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

New Post(0)