Php sample code for file download and support for Chinese file names

Source: Internet
Author: User
Tags php sample code
Php sample code for file download and support for Chinese file names

  1. /*----------------

  2. * $ FileName is the file name, required
  3. * $ FilePath is the file path. optional. it can be a relative or absolute path.
  4. * @ Path can only consist of English letters and data, and cannot contain Chinese characters
  5. * @ Edit finishing: bbs.it-home.org
  6. ------------------*/
  7. Header ("Content-type: text/html; charset = utf-8 ");
  8. If (strlen ($ FileName) <= 3) {echo "Download failed: your downloaded file information is incorrect"; return ;}
  9. $ FileName = iconv ("UTF-8", "gb2312", $ FileName); // convert the file name format to prevent Chinese garbled characters
  10. // Start to determine the path
  11. If (! Is_null ($ FilePath) & strlen ($ FilePath)> 1 ){
  12. If (substr ($ FilePath,) = '/') {// Determine whether the path is an absolute path
  13. $ FilePath = $ _ SERVER ['document _ root']. $ FilePath;
  14. }
  15. If (substr ($ FilePath,-1 )! = "/") {// Check whether the end is/
  16. $ FilePath = $ FilePath .'/';
  17. }
  18. If (is_numeric (strpos ($ FilePath, ": \") {// check whether the path is absolute
  19. $ FilePath = str_replace ("/", "\", $ FilePath );
  20. }
  21. } Elseif (strlen ($ FilePath) = 1 & $ FilePath! = "/"){
  22. $ FilePath = $ FilePath ."/";
  23. } Else {
  24. $ FilePath = "";
  25. }
  26. If (! File_exists ($ FilePath. $ FileName )){
  27. Echo "Download failed: the file to be downloaded is not found"; return;
  28. }
  29. /*-------------
  30. Send download-related header information
  31. ------------- = */
  32. Header ("Content-type: application/octet-stream ");
  33. Header ("Accept-Ranges: bytes"); // returns the value in bytes.
  34. Header ("Accept-Length: $ FileSize"); // returns the file size.
  35. Header ("Content-Disposition: attachment; filename =". $ FileName); // The pop-up dialog box on the client, with the corresponding file name

  36. /*-------------

  37. Start download
  38. ------------- = */
  39. $ FileSize = filesize ($ FilePath. $ FileName );
  40. $ File = fopen ($ FilePath. $ FileName, "r"); // open the File
  41. $ FileBuff = 512;
  42. While ($ FileSize> = 0 ){
  43. $ FileSize-= $ FileBuff;
  44. Echo fread ($ File, $ FileBuff );
  45. }
  46. Fclose ($ File );
  47. }
  48. ?>

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.