PHP header function Implementation download short point renewal program

Source: Internet
Author: User
Tags ranges strlen

For example: Download the output file size, file name, etc.
The premise is that the. htaccess file is configured to add a sentence
Setenv No-gzip Dont-vary
is to not compress files against the file

Example 1

The code is as follows Copy Code

<?php
function Download ($file _dir, $file _name)
Parameter description:
File_dir: directory where files reside
file_name: filename
{
$file _dir = Chop ($file _dir);//Remove extra spaces from the path
The path to the file to download
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 if the file you want to download exists
if (!file_exists ($file _path))
{
Echo ' Sorry, the file you are downloading 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 ', ' filename ');
?>

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 location to EOF and writes the result to the output buffer

The above two examples of Chinese support is not good, the following function is a good solution to this problem

The code is as follows Copy Code

<?php

/**

* Php-http Breakpoint Continuous Transmission 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);

$size 2 = $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. '-'. $size 2. ' /'. $size);

} else {

Header (' Content-length: '. $size);

Header (' Content-range:bytes 0-'. $size 2. ' /'. $size);

}

Header (' accenpt-ranges:bytes ');

Header (' Application/octet-stream ');

Header ("Cache-control:public");

Header ("Pragma:public");

Solve Chinese garbled problem 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*/

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.