Small example of PHP file download_PHP tutorial

WBOY
Release: 2016-07-13 10:44:09
Original
1197 people have browsed it

This file download example is very detailed. It combines the header function and the while fread function to read out the file in segments and then sends it to the client. It can be regarded as a standard file download example.

A small example of PHP file download
/*================================================ ======
$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, and cannot contain Chinese
If you have any questions, please contact the blogger to point it out
================================================== ====*/

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
The code is as follows
 代码如下 复制代码

header("Content-type: text/html;charset=utf-8");
if(strlen($FileName)<=3){echo "下载失败:你所以下载的文件信息有误";return;}
$FileName=iconv("utf-8","gb2312",$FileName);//进行文件名格式转换,以防中文乱码
//开始判断路径
if(!is_null($FilePath)&&strlen($FilePath)>1){
 
  if(substr($FilePath,0,1)=='/'){//判断是否为绝对路径
 
   $FilePath=$_SERVER['DOCUMENT_ROOT'].$FilePath;
  
    }
  if(substr($FilePath,-1)!="/"){//检查最后是否为 / 结尾
 
   $FilePath=$FilePath.'/';
  
    }
  if(is_numeric(strpos($FilePath,":"))){//检查是否为绝对路径
 
   $FilePath=str_replace("/","",$FilePath);
  
    }
   }elseif(strlen($FilePath)==1&&$FilePath!="/"){
   
    $FilePath=$FilePath."/";
   
   }else{
   
    $FilePath="";
   
  }
  if(!file_exists($FilePath.$FileName)){
  
   echo"下载失败:所要下载的文件未找到";return;
  
   }
  /*================================================
   发送下载相关的头部信息
  =================================================*/
 
  header("Content-type: application/octet-stream");
 
  header("Accept-Ranges: bytes");//按照字节大小返回
 
  header("Accept-Length: $FileSize");//返回文件大小
 
  header("Content-Disposition: attachment; filename=".$FileName);//这里客户端的弹出对话框,对应的文件名
 
  /*================================================
   开始下载相关
  =================================================*/ 

$FileSize=filesize($FilePath.$FileName);
 
  $File=fopen($FilePath.$FileName,"r");//打开文件
 
  $FileBuff=512;
 
  while($FileSize>=0){
  
   $FileSize-=$FileBuff;
  
   echo fread($File,$FileBuff);
  
  }
  
  fclose($File);
 }

?>

Copy code

header("Content-type: text/html;charset=utf-8");

//Start to 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 file size header("Content-Disposition: attachment; filename=".$FileName);//The pop-up dialog box of the client here, the corresponding file name /*================================================ Start downloading related content =================================================* / $FileSize=filesize($FilePath.$FileName); $File=fopen($FilePath.$FileName,"r");//Open file $FileBuff=512; while($FileSize>=0){ $FileSize-=$FileBuff; echo fread($File,$FileBuff); } fclose($File); } ?> Summary This download example also supports Chinese names, and UFT8 encoding conversion is performed at the beginning of the file. http://www.bkjia.com/PHPjc/633123.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/633123.htmlTechArticleThis file download example is very detailed. It combines the header function and the while fread function to segment the file and read it out. Then it is sent to the client, which can be regarded as a standard file download implementation...
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template