Sample code for php to implement file download and support Chinese file names
Release: 2016-07-25 08:55:53
Original
788 people have browsed it
-
-
/*---------------- - * $FileName is the file name, must be passed
- * $FilePath is the file path .Optional, it can be a relative path or an absolute path
- * @The path can only be composed of English and data, not Chinese
- * @Edited by: bbs.it-home.org
- --------- ---------*/
-
- header("Content-type: text/html;charset=utf-8");
- if(strlen($FileName)<=3){echo "Download failed : The file information you downloaded is incorrect";return;}
- $FileName=iconv("utf-8","gb2312",$FileName);//Convert the file name format to prevent Chinese garbled characters
- //Start Determine the path
- if(!is_null($FilePath)&&strlen($FilePath)>1){
- if(substr($FilePath,0,1)=='/'){//Determine whether it is an absolute path
- $FilePath =$_SERVER['DOCUMENT_ROOT'].$FilePath;
- }
- if(substr($FilePath,-1)!="/"){//Check whether the end is /
- $FilePath=$FilePath.'/' ;
- }
- if(is_numeric(strpos($FilePath,":"))){//Check whether it is an absolute path
- $FilePath=str_replace("/","",$FilePath);
- }
- }elseif( strlen($FilePath)==1&&$FilePath!="/"){
- $FilePath=$FilePath."/";
- }else{
- $FilePath="";
- }
- if(!file_exists($FilePath. $FileName)){
- echo "Download failed: The file to be downloaded was not found"; return;
- }
- /*-------------
- Send download related header information
- -- -----------=*/
- header("Content-type: application/octet-stream");
- header("Accept-Ranges: bytes");//Return according to byte size
- header("Accept-Length: $FileSize");//Return the file size
- header("Content-Disposition: attachment; filename=".$FileName);//The client's pop-up dialog box here, the corresponding file name< /p>
/*-------------
- Start downloading related
- -------------=*/
- $FileSize=filesize( $FilePath.$FileName);
- $File=fopen($FilePath.$FileName,"r");//Open the file
- $FileBuff=512;
- while($FileSize>=0){
- $FileSize-=$ FileBuff;
- echo fread($File,$FileBuff);
- }
- fclose($File);
- }
- ?>
-
Copy code
|
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
-
2024-10-22 09:46:29
-
2024-10-13 13:53:41
-
2024-10-12 12:15:51
-
2024-10-11 22:47:31
-
2024-10-11 19:36:51
-
2024-10-11 15:50:41
-
2024-10-11 15:07:41
-
2024-10-11 14:21:21
-
2024-10-11 12:59:11
-
2024-10-11 12:17:31