Php forced File Download instead of user-defined function sharing in a browser _ php instance

Source: Internet
Author: User
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;
}

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.