Php UDF code for Force File Download-php Tutorial

Source: Internet
Author: User
Php user-defined function code for forced File Download

If you want to download the file directly when you click the corresponding link instead of displaying it on the webpage, you need to set the header information forcibly.

Share a piece of php function implementation code that does not generate garbled code to implement forced download of files.

For example, php implements the code for forced file download.

  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. }

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.