This article mainly introduces php forced File Download instead of sharing Custom functions opened in a browser, for more information, see the section, text document, webpage, mp3, pdf, and other content that we want to download when you click the corresponding link, instead of displaying the content on the webpage, you need to set the header information forcibly. The following is a php function implementation code that does not generate garbled characters. For other programming languages, you can refer to the Code for implementation.
The Code is as follows:
/**
* Downloader
*
* @ Param $ archivo
* Path al archivo
* @ Param $ downloadfilename
* (Null | string) el nombre que queres usar para el archivo que se va a descargar.
* (Si no lo especificas usa el nombre actual del archivo)
*
* @ Return file stream
*/
Function download_file ($ archivo, $ downloadfilename = null ){
If (file_exists ($ archivo )){
$ Downloadfilename = $ downloadfilename! = Null? $ Downloadfilename: basename ($ archivo );
Header ('content-Description: File Transfer ');
Header ('content-Type: application/octet-stream ');
Header ('content-Disposition: attachment; filename = '. $ downloadfilename );
Header ('content-Transfer-Encoding: binary ');
Header ('expires: 0 ');
Header ('cache-Control: must-revalidate, post-check = 0, pre-check = 0 ');
Header ('pragma: public ');
Header ('content-Length: '. filesize ($ archivo ));
Ob_clean ();
Flush ();
Readfile ($ archivo );
Exit;
}
}