PhpHeader function to download the short-point resume program _ PHP Tutorial

Source: Internet
Author: User
The phpHeader function downloads the short-point resume program. This article will summarize several functions for downloading the short-point resume program. these functions mainly use the php header function. if you need to know about these functions, you can refer to them. For example, download this article to summarize several functions for downloading the short-point resumable Upload program. these functions mainly use the php header function. if you need to know about these functions, you can refer to them.

For example, the size and file name of the downloaded file are output during download.
The premise is that you need to add a sentence for the configuration of the. htaccess file.
SetEnv no-gzip dont-vary
That is, files are not compressed.

Example 1

The code is as follows:

Function download ($ file_dir, $ file_name)
// Parameter description:
// File_dir: Directory of the file
// File_name: file name
{
$ File_dir = chop ($ file_dir); // remove unnecessary spaces in the path
// Obtain the path of the object 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 the object to be downloaded 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 parameters', 'filename ');
?>

Example 2

The code is as follows:
$ 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= ");
Fpassthru ($ fp );


The fpassthru () function outputs all the 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 instances have poor support for Chinese characters. the following function can solve this problem well.

The code is as follows:

 

/**

* Breakpoint resume implementation of PHP-HTTP

* @ Param string $ path: path of the file

* @ 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 Chinese garbled characters during download 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 */


Bytes. For example, download...

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.