Processing file loading, file downloading, and other related functions

Source: Internet
Author: User
Provides various official and user-released code examples and code reference. You are welcome to exchange and learn how to process file loading, file download, and other related functions.

Class http {

/**
* Download an object through http
*
* Reads a file and send a header to force download it.
* @ Copyright www.doophp.com
* @ Param string $ file_str File name with absolute path to it
* @ Param bool $ isLarge If True, the large file will be read chunk by chunk into the memory.
* @ Param string $ rename Name to replace the file name that wocould be downloaded
*/
Public static function download ($ file, $ isLarge = false, $ rename = NULL ){

If (headers_sent () return false;

If (! $ File ){
Exit ('error 404: The file not found! ');
}

If ($ rename = NULL ){
If (strpos ($ file, '/') ==false & strpos ($ file, '\') == false)
$ Filename = $ file;
Else {
$ Filename = basename ($ file );
}
} Else {
$ Filename = $ rename;
}

Header ('content-Description: File Transfer ');
Header ('content-Type: application/octet-stream ');
Header ("Content-Disposition: attachment; filename = \" $ filename \"");
Header ('expires: 0 ');
Header ('cache-Control: must-revalidate, post-check = 0, pre-check = 0 ');
Header ('pragma: public ');
Header ('content-Length: '. filesize ($ file ));
Ob_clean ();
Flush ();

If ($ isLarge)
Self: readfileChunked ($ file );
Else
Readfile ($ file );
}

/**
* Read a file and display its content chunk by chunk
*
* @ Param string $ filename
* @ Param bool $ retbytes
* @ Return mixed
*/
Private function readfileChunked ($ filename, $ retbytes = true, $ chunkSize = 1024 ){

$ Buffer = '';
$ Cnt = 0;

$ Handle = fopen ($ filename, 'rb ');
If ($ handle = false ){
Return false;
}
While (! Feof ($ handle )){
$ Buffer = fread ($ handle, $ chunkSize );
Echo $ buffer;
Ob_flush ();
Flush ();
If ($ retbytes ){
$ Cnt + = strlen ($ buffer );
}
}
$ Status = fclose ($ handle );
If ($ retbytes & $ status ){
Return $ cnt; // return num. bytes delivered like readfile () does.
}

Return $ status;
}

/**
* HTTP status information array
*
* @ Var array
*/
Protected static $ _ httpStatus = array (

'20140901' => 'contine ',
'20140901' => 'switching Protocols ',

'20140901' => 'OK ',
'000000' => 'created ',
'20140901' => 'accepted ',
'000000' => 'non-Authoritative information ',
'000000' => 'no content ',
'000000' => 'reset content ',
'000000' => 'partial content ',

'000000' => 'Multiple Choices ',
'20140901' => 'moved Permanently ',
'20140901' => 'found ',
'000000' => 'see Other ',
'20140901' => 'not modified ',
'20140901' => 'use proxy ',
'000000' => '(Unused )',
'20140901' => 'temporary redirect ',

'20140901' => 'bad request ',
'20140901' => 'authorized ',
'000000' => 'payment required ',
'000000' => 'forbidden ',
'20140901' => 'not Found ',
'20140901' => 'method Not allowed ',
'20140901' => 'not accesstable ',
'20140901' => 'proxy Authentication required ',
'20140901' => 'request timeout ',
'20140901' => 'canonicalization ',
'000000' => 'gione ',
'20140901' => 'length required ',
'20140901' => 'precondition failed ',
'000000' => 'request Entity Too Large ',
'20140901' => 'request-URI Too long ',
'20140901' => 'unororted Media type ',
'20140901' => 'requested Range Not satisfiable ',
'20140901' => 'pectation failed ',

'000000' => 'internal Server error ',
'20140901' => 'not implemented ',
'20140901' => 'bad gateway ',
'20140901' => 'service unavailable ',
'20140901' => 'Gateway timeout ',
'20140901' => 'HTTP Version Not Supported ted ',
);

/**
* Set HTTP status information
*
* @ Access public
* @ Param string $ code HTTP status code
* @ Param string $ name HTTP status information
* @ Return string
*/
Public static function httpStatus ($ code, $ text = null ){
// Obtain the http protocol
$ Protocol = isset ($ _ SERVER ['server _ Protocol'])? $ _ SERVER ['server _ Protocol']: 'http/123 ';
$ Text = is_null ($ text )? Self: $ _ httpStatus [$ code]: $ text;
$ Status = "$ protocol $ code $ text ";

Header ($ status );
}
}

AD: truly free, domain name + VM + enterprise mailbox = 0 RMB

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.