


PHP implements downloading of files in multiple formats and solves garbled code problems
I have been working on a project for the past few days and I have encountered a problem. It is easy to download general files, but it is more difficult to download files in Chinese and in multiple formats. I have been fixing the bug for a long time,
It is finally completed. , the following points need to be noted:
1. For files with Chinese file names, remember to transcode to prevent Chinese garbled characters, because the encoding of most hosts is GB2312, and we use It is UTF-8,
so must use the iconv() function to transcode before operation to prevent the file from being found and reporting an error.
2. You need to judge the format of the file to be downloaded, and then write the header() function according to the format.
The specific code is as follows:
<?php header("Content-type:text/html;charset=utf-8"); $filename = "大学英语选课说明.docx"; //文件名 $filename = iconv("utf-8","gb2312//IGNORE",$filename); //转码 $file = "upload/".$filename; //文件路径 $len = filesize($file); //文件大小 $file_extension = strtolower(substr(strrchr($filename,"."),1)); //文件后缀名 switch( $file_extension ) //判断文件类型 { case "pdf": $ctype="application/pdf"; break; case "exe": $ctype="application/octet-stream"; break; case "zip": $ctype="application/zip"; break; case "docx": case "doc": $ctype="application/msword"; break; case "xls": $ctype="application/vnd.ms-excel"; break; case "ppt": $ctype="application/vnd.ms-powerpoint"; break; case "gif": $ctype="image/gif"; break; case "png": $ctype="image/png"; break; case "jpeg": case "jpg": $ctype="image/jpg"; break; case "mp3": $ctype="audio/mpeg"; break; case "wav": $ctype="audio/x-wav"; break; case "mpeg": case "mpg": case "mpe": $ctype="video/mpeg"; break; case "mov": $ctype="video/quicktime"; break; case "avi": $ctype="video/x-msvideo"; break; case "php": case "htm": case "html": die("<b>Cannot be used for ". $file_extension ." files!</b>"); break; default: $ctype="application/force-download"; } header("Pragma: public"); header("Expires: 0"); header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); header("Cache-Control: public"); header("Content-Description: File Transfer"); header("Content-Type: $ctype"); $header="Content-Disposition: attachment; filename=".$filename.";"; header($header); header("Content-Transfer-Encoding: binary"); header("Content-Length: ".$len); readfile($file); ?>
To adapt to different browsers and avoid Chinese garbled errors, I found a piece of code on the Internet that can be avoided. The code is as follows:
<?php <span style="white-space:pre"> </span>$ua = $_SERVER["HTTP_USER_AGENT"]; <span style="white-space:pre"> </span>$filename = "中文 文件名.txt"; <span style="white-space:pre"> </span>$encoded_filename = urlencode($filename); <span style="white-space:pre"> </span>$encoded_filename = str_replace("+", "%20", $encoded_filename); <span style="white-space:pre"> </span>header('Content-Type: application/octet-stream'); <span style="white-space:pre"> </span>if (preg_match("/MSIE/", $ua)) { <span style="white-space:pre"> </span>header('Content-Disposition: attachment; filename="' . $encoded_filename . '"'); <span style="white-space:pre"> </span>} else if (preg_match("/Firefox/", $ua)) { <span style="white-space:pre"> </span>header('Content-Disposition: attachment; filename*="utf8\'\'' . $filename . '"'); <span style="white-space:pre"> </span>} else { <span style="white-space:pre"> </span>header('Content-Disposition: attachment; filename="' . $filename . '"'); <span style="white-space:pre"> </span>} ?>
Me The reason why this code is not used is that I tried the code I wrote above on browsers such as Firefox, IE, Opera, 360, etc., and no garbled errors occurred due to browser problems
. I think it is transcoding. The problem of garbled Chinese characters is basically solved.
My own experience, for reference only!
Copyright Statement: This article is an original article by the blogger and may not be reproduced without the blogger's permission.
The above has introduced how PHP can download files in multiple formats and solve the problem of garbled characters, including the content. I hope it will be helpful to friends who are interested in PHP tutorials.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



[SpringBoot] Passing parameters in the Header through Feign calls How to pass Header parameters through Feign Problem description When we use Feign to request the Api interface of another service in Spring Cloud, there is a need to pass the parameters in the Header. If no special processing is done, it will The parameters in the Header will be lost. Solution 1: Pass it through @RequestHeader(name="headerName"). For example: Feign is defined as follows @FeignClient(name="service-name")pub

The Linux header refers to the beginning of a file or data stream, which is used to contain metadata about the content. By correctly writing and using Header files, developers can better utilize system resources and improve code readability and Maintainability.

PHP is a powerful programming language that can be used to create dynamic websites and web applications. One of the most powerful features is PHP’s header() method. In this article, we will explore how to use PHP’s header() method to adjust web pages.

How to implement jump in php header: 1. Use "Header("Location:$url");" syntax to implement jump; 2. Use if judgment to implement jump, with jump statements such as "if($_COOKIE[" u_type"]){ header('location:register.php'); } else{ setcookie('u_type','1','86400*360');".

Differences: 1. The head tag is used to define the head of the document, which is a container for all head elements, and the header tag is used to define the header (introduction information) of the document; 2. All browsers support the head tag, and older versions of browsers None of the browsers support the header tag, and browsers such as IE9+ and above are required to support the header tag.

When the HMD Skyline(available on Amazon for $499) was launched last month, it was released in two colors - Neon Pink and Twisted Black. They are now joined by a third color dubbed Blue Topaz. HMD Global has also announced an official case for the ph

php提交表单通过后,弹出的对话框怎样在当前页弹出php提交表单通过后,弹出的对话框怎样在当前页弹出而不是在空白页弹出?想实现这样的效果:而不是空白页弹出:------解决方案--------------------如果你的验证用PHP在后端,那么就用Ajax;仅供参考:HTML code

The differences are: 1. div is a block-level element, and span is an inline element; 2. div will automatically occupy a line, while span will not automatically wrap; 3. div is used to wrap larger structures and layouts, and span is used to wrap Text or other inline elements; 4. div can contain other block-level elements and inline elements, and span can contain other inline elements.
