Home >Backend Development >PHP Tutorial >PHP Header function implements download short-click resume program_PHP tutorial

PHP Header function implements download short-click resume program_PHP tutorial

WBOY
WBOYOriginal
2016-07-20 11:11:521188browse

This article will summarize several program functions for implementing download short-point resuming. These functions mainly use the header function of PHP. Friends who need to know more can refer to it.

For example: when downloading, output the download file size, file name, etc.
The premise is that the configuration of the .htaccess file needs to add a sentence
SetEnv no-gzip dont-vary
That is, the file will not be compressed

Example 1

The code is as follows
Copy code
 代码如下 复制代码

function download($file_dir,$file_name)
//参数说明:
//file_dir:文件所在目录
//file_name:文件名
{
$file_dir = chop($file_dir);//去掉路径中多余的空格
//得出要下载的文件的路径
if($file_dir != '')
{
$file_path = $file_dir;
if(substr($file_dir,strlen($file_dir)-1,strlen($file_dir)) != '/')
$file_path .= '/';
$file_path .= $file_name;
} else {
$file_path = $file_name;
}
//判断要下载的文件是否存在
if(!file_exists($file_path))
{
echo '对不起,你要下载的文件不存在。';
return false;
}
$file_size = filesize($file_path);
@header("Cache-control: public");
@header("Pragma: public");
//header("Content-Encoding: gzip");
@header("Content-Type: application/octetstream");
header("Content-Length: $file_size");
Header("Accept-Ranges: bytes");
header("Content-Disposition: attachment; filename=".$file_name);
$fp = fopen($file_path,"r");
fpassthru($fp);
return true;
}
download('路径参数','文件名');
?>

function download($file_dir,$file_name)

//Parameter description:
代码如下 复制代码
$fname = './MMLDZG.mp3';
$fp = fopen($fname,'rb');
$fsize = filesize($fname);
if (isset($_SERVER['HTTP_RANGE']) && ($_SERVER['HTTP_RANGE'] != "") && preg_match("/^bytes=([0-9]+)-$/i", $_SERVER['HTTP_RANGE'], $match) && ($match[1] < $fsize)) {
$start = $match[1];
} else {
$start = 0;
}
@header("Cache-control: public"); @header("Pragma: public");
if ($star--> 0) { 
    fseek($fp, $start); 
    Header("HTTP/1.1 206 Partial Content"); 
    Header("Content-Length: " . ($fsize - $start)); 
    Header("Content-Ranges: bytes" . $start . "-" . ($fsize - 1) . "/" . $fsize); 
} else { 
    header("Content-Length: $fsize"); 
    Header("Accept-Ranges: bytes"); 

@header("Content-Type: application/octet-stream"); 
@header("Content-Disposition: attachment;filename=mmdld.mp3"); 
fpassthru($fp); 
 
//file_dir: file location Directory//file_name:File name{ $file_dir = chop($file_dir);//Remove extra spaces in the path//Get the path of the file to be downloaded if($file_dir != '') { $file_path = $file_dir; if(substr($file_dir,strlen($file_dir)-1,strlen($file_dir)) != '/ ') $file_path .= '/'; $file_path .= $file_name; } else { $file_path = $file_name; } //Determine whether to download Whether the file exists if(!file_exists($file_path)) { echo 'Sorry, the file you want to download does not exist. '; return false; } $file_size = filesize($file_path); @header("Cache-control: public"); @header("Pragma: public" ); //header("Content-Encoding: gzip"); @header("Content-Type: application/octetstream"); ​​header("Content-Length: $file_size"); Header("Accept-Ranges: bytes"); header("Content-Disposition: attachment; filename=".$file_name); $fp = fopen($file_path,"r"); fpassthru($fp); return true; } download('path parameter','file name'); ?> tr> Example 2
The code is as follows Copy code
$fname = './MMLDZG.mp3'; $fp = fopen($fname,'rb'); $fsize = filesize($fname); if (isset($_SERVER['HTTP_RANGE']) && ($_SERVER['HTTP_RANGE'] != "") && preg_match("/^bytes=([0-9] +)-$/i", $_SERVER['HTTP_RANGE'], $match) && ($match[1] < $fsize)) {<🎜> $start = $match[1]; <🎜>} else {<🎜> $start = 0; <🎜>} <🎜>@header("Cache-control: public"); @header("Pragma: public"); <🎜>if ($star--> 0 ) { fseek($fp, $start); Header("HTTP/1.1 206 Partial Content"); Header("Content-Length: " . ($fsize - $start)); Header("Content-Ranges: bytes" . $start . "-" . ($fsize - 1) . "/" . $fsize); } else { header("Content-Length : $fsize"); Header("Accept-Ranges: bytes"); } @header("Content-Type: application/octet-stream"); ​​@header(" Content-Disposition: attachment;filename=mmdld.mp3"); fpassthru($fp);


The fpassthru() function outputs all remaining data at the file pointer.

This function reads the given file pointer from the current position to EOF, and writes the result to the output buffer

The above two examples do not support Chinese well, the following function This problem is solved very well

The code is as follows
 代码如下 复制代码

 

  /**

  * PHP-HTTP断点续传实现

  * @param string $path: 文件所在路径

  * @param string $file: 文件名

  * @return void

  */

  function download($path,$file) {

  $real = $path.'/'.$file;

  if(!file_exists($real)) {

  return false;

  }

  $size = filesize($real);

  $size2 = $size-1;

  $range = 0;

  if(isset($_SERVER['HTTP_RANGE'])) {

  header('HTTP /1.1 206 Partial Content');

  $range = str_replace('=','-',$_SERVER['HTTP_RANGE']);

  $range = explode('-',$range);

  $range = trim($range[1]);

  header('Content-Length:'.$size);

  header('Content-Range: bytes '.$range.'-'.$size2.'/'.$size);

  } else {

  header('Content-Length:'.$size);

  header('Content-Range: bytes 0-'.$size2.'/'.$size);

  }

  header('Accenpt-Ranges: bytes');

  header('application/octet-stream');

  header("Cache-control: public");

  header("Pragma: public");

  //解决在IE中下载时中文乱码问题

  $ua = $_SERVER['HTTP_USER_AGENT'];

  if(preg_match('/MSIE/',$ua)) {

  $ie_filename = str_replace('+','%20',urlencode($file));

  header('Content-Dispositon:attachment; filename='.$ie_filename);

  } else {

  header('Content-Dispositon:attachment; filename='.$file);

  }

  $fp = fopen($real,'rb+');

  fseek($fp,$range);

  while(!feof($fp)) {

  set_time_limit(0);

  print(fread($fp,1024));

  flush();

  ob_flush();

  }

  fclose($fp);

  }

  /*End of PHP*/

Copy code
  /**

* PHP-HTTP breakpoint resume transfer implementation * @param string $path: The path where the file is located * @param string $file: File name * @return void*/ function download($path,$file) { $real = $path.'/'.$file;
 if(!file_exists($real)) {
 return false;
 } $size = filesize($real); $size2 = $size-1; $range = 0; if(isset($ _SERVER['HTTP_RANGE'])) { header('HTTP /1.1 206 Partial Content'); $range = str_replace('=','-',$_SERVER[' HTTP_RANGE']); $range = explode('-',$range); $range = trim($range[1]); header( 'Content-Length:'.$size); header('Content-Range: bytes '.$range.'-'.$size2.'/'.$size); } else { header('Content-Length:'.$size); header('Content-Range: bytes 0-'.$size2.'/'.$size ); } header('Accenpt-Ranges: bytes'); header('application/octet-stream'); header ("Cache-control: public");header("Pragma: public"); //Solve the problem of Chinese garbled characters when downloading in IE $ua = $_SERVER['HTTP_USER_AGENT'];if(preg_match('/MSIE/',$ua)) {$ie_filename = str_replace('+','%20', urlencode($file)); header('Content-Dispositon:attachment; filename='.$ie_filename); } else { header('Content- Dispositon:attachment; filename='.$file); } $fp = fopen($real,'rb+'); fseek($fp,$ range);while(!feof($fp)) {set_time_limit(0);print(fread($fp,1024)); Flush(); ob_flush(); } fclose($fp); *End of PHP*/ http://www.bkjia.com/PHPjc/444606.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/444606.htmlTechArticleThis article summarizes several functions for downloading short-point resuming programs. These functions are mainly used Friends who need to know more about the header function of php can refer to it. For example: Download...
Statement:
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