PHP uses code to download files. when reading PHP code to download files, we generally call URLs to download files. However, this method is not available when ie can identify files opened, this article will introduce how to download PHP files. For more information, refer to PHP code to download files. For more information, see PHP code to download files, we generally call URLs to download files, but this method cannot be used when ie can identify files opened, such as downloading an image or html webpage, in this case, programming is required. the following php code can solve the problem :? If (empty ($ _ GET [we generally call URLs for download, but this method cannot be used when ie can identify opened files, for example, to download an image or html webpage, you need to program it. the following php code can be used:
The code is as follows:
If (empty ($ _ GET ['filename']) | empty ($ _ GET ['filedir']) | empty ($ _ GET ['fileid']) {
Echo 'script alert ("illegal connection! "); Location. replace (" index. php ") script '; exit ();
}
$ File_name = $ _ GET ['filename'];
$ File_dir = $ _ GET ['filedir'];
$ FileId = $ _ GET ['fileid'];
$ File_dir = $ file_dir ."/";
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 ();
}
?>