PHP Tutorial. application instance 6. The PHP program for secure file download is as follows: export file_nameinfo_check.exe; $ file_dirpublicwwwdownload; if (! File_exists ($ file_dir. $ file_name) {check whether the file is stored in PHP for secure file download
The procedure is as follows:
$ File_name = "info_check.exe ";
$ File_dir = "/public/www/download /";
If (! File_exists ($ file_dir. $ file_name) {// check whether the file exists
Echo "file not found ";
Exit;
} Else {
$ File = fopen ($ file_dir. $ file_name, "r"); // open the file
// Input 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", the source code will change a little. 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 "file not found ";
} Else {
Header ("Content-type: application/octet-stream ");
Header ("Content-Disposition: attachment; filename =". $ file_name );
While (! Feof ($ file )){
Echo fread ($ file, 50000 );
}
Fclose ($ file );
}
In this way, you can use PHP to directly output files.
The program is as follows: $ file_name = info_check.exe; $ file_dir =/public/www/download/; if (! File_exists ($ file_dir. $ file_name) {// check whether the file is stored...