PHP Mandatory File Download Custom function code

Source: Internet
Author: User

Sometimes when you want to download the link directly instead of on the page, you need to force header information to be set.

Share a piece of PHP function implementation code that does not produce garbled characters, implement the mandatory download of the file.

example, the PHP implementation file forces the download of the code.

  1. /**
  2. * Downloader
  3. *
  4. * @param $archivo
  5. * Path Al Archivo
  6. * @param $downloadfilename
  7. * (null|string) el nombre que queres usar para el archivo que se va a descargar.
  8. * (Si no lo especificas usa el nombre actual del archivo)
  9. *
  10. * @return File stream
  11. */bbs.it-home.org
  12. function Download_file ($archivo, $downloadfilename = null) {
  13. if (file_exists ($archivo)) {
  14. $downloadfilename = $downloadfilename!== null? $downloadfilename: basename ($archivo);
  15. Header (' Content-description:file Transfer ');
  16. Header (' Content-type:application/octet-stream ');
  17. Header (' content-disposition:attachment; Filename= '. $downloadfilename);
  18. Header (' content-transfer-encoding:binary ');
  19. Header (' expires:0 ');
  20. Header (' Cache-control:must-revalidate, post-check=0, pre-check=0 ');
  21. Header (' Pragma:public ');
  22. Header (' Content-length: '. FileSize ($archivo));
  23. Ob_clean ();
  24. Flush ();
  25. ReadFile ($archivo);
  26. Exit
  27. }
  28. }
Copy Code
  • Related Article

    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.