I designed a php file to download the current file, but downloaded the php source file. why? After I download a.txt, open the txt file and find that the content in the file is not a.txt, but other text content in the php file besides the php code. how can I solve this problem? & Lt ;! DOCTYPE & nbsp; h designed a php file to download the current file, but downloaded the php source file. why?
After I download a.txt, open the txt file and find that the content in the file is not a.txt, but other text content in the php file besides the php code. how can I solve this problem?
Data Download
$ File_name = "a.txt ";
$ File_dir = "./";
If (! File_exists ($ file_dir. $ file_name )){
Echo "file not found ";
Exit;
} Else {
$ File = fopen ($ file_dir. $ file_name, "r"); // open the file
Echo ($ file_dir. $ file_name );
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 );
}
?>
Share:
------ Solution --------------------
$ File_name = "a.txt ";
$ File_dir = "./";
If (! File_exists ($ file_dir. $ file_name )){
Echo "file not found ";
Exit;
} Else {
$ File = fopen ($ file_dir. $ file_name, "r"); // open the file
// Echo ($ file_dir. $ file_name );
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 );
Readfile ($ file_dir. $ file_name );
}