This article introduces php stream download, which supports multipart and resumable download. If you need it, you can check it. Code example: Copy code dowmfiledirname({file=}.'nokia-alwayshere.mp3 '; the file to be downloaded, absolute or relative to $ dowmName 'nokia-Alw
This article introduces php stream download, which supports multipart and resumable download. If you need it, you can check it. The Code is as follows: $ dowmFile = dirname (_ FILE _). '/Nokia-Always Here.mp3'; // the FILE to be downloaded, absolute or relative to $ dowmName = 'nokia-Alw
This article introduces php stream download, which supports multipart and resumable download. If you need it, you can check it. The Code is as follows:
$ DowmFile = dirname (_ FILE _). '/Nokia-Always Here.mp3'; // the object to be downloaded, either absolute or relative
$ DowmName = 'nokia-Always Here.mp3 ′;
Ob_start ();
Getlocalfile ($ dowmFile, $ dowmName );
Flush ();
Ob_flush ();
Function getlocalfile ($ fname, $ filename = "){
$ Fsize = filesize ($ fname );
Header ('cache-Control: public ');
Header ('pragma: public ');
Header ('Accept-Ranges: bytes ');
Header ('Connection: close ');
Header ('content-Type: '. MIMEType ($ fname ));
// Header ('content-Type: application/octet-stream ');
If (isset ($ filename {0 })){
Header ('content-Disposition: attachment; filename = '. $ filename );
}
If ($ fp = @ fopen ($ fname, 'rb ')){
$ Start = 0;
$ End = $ fsize;
$ IsRange = isset ($ _ SERVER ['HTTP _ range']) & ($ _ SERVER ['HTTP _ range']! = ");
If ($ isRange ){
Preg_match ('/^ bytes = ([0-9] *)-([0-9] *) $/I', $ _ SERVER ['HTTP _ range'], $ match );
$ Start = $ match [1];
$ End = $ match [2];
$ Isset_start = isset ($ start {0 });
$ Isset_end = isset ($ end {0 });
If ($ isset_start & $ isset_end ){
// Multipart download
If ($ start >=$ fsize | $ start <0 | $ start> $ end ){
$ Start = 0;
$ End = $ fsize;
} Else if ($ end >=$ fsize ){
$ End = $ fsize-$ start;
} Else {
$ End-= $ start-1;
}
} Else if ($ isset_start &&! $ Isset_end ){
// Specify the position to end
If ($ start >=$ fsize | $ start <0 ){
$ Start = 0;
$ End = $ fsize;
} Else {
$ End = $ fsize-$ start;
}
} Else if (! $ Isset_start & $ isset_end ){
// Last n Bytes
$ End = $ end> $ fsize? $ Fsize: $ end;
$ Start = $ fsize-$ end;
} Else {
$ Start = 0;
$ End = $ fsize;
}
}
If ($ isRange ){
Fseek ($ fp, $ start );
Header ('HTTP/1.1 206 Partial content ');
Header ('content-Length: '. $ end );
Header ('content-Ranges: bytes '. $ start.'-'. ($ end + $ start-1).'/'. $ fsize );
} Else {
Header ('content-Length: '. $ fsize );
}
If (function_exists ('fpassthru') & ($ end + $ start) ==$ fsize ){
Fpassthru ($ fp );
} Else {
Echo fread ($ fp, $ end );
}
} Else {
Header ('content-Length: '. $ fsize );
Readfile ($ fname );
}
// @ Header ("Content-Type:". mime_content_type ($ fname ));
}
Function MIMEType ($ fname ){
$ FileSuffix = strtolower (substr ($ fname, strrpos ($ fname, '.') + 1 ));
Switch ($ fileSuffix ){
Case 'av ':
Return 'video/msvideo ';
Case 'vps ':
Return 'video/x-ms-wmv ';
Case 'txt ':
Return 'text/plain ';
Case 'htm ':
Case 'html ':
Case 'php ':
Return 'text/html ';
Case 'css ':
Return 'text/css ';
Case 'js ':
Return 'application/javascript ';
Case 'json ':
Case 'xml ':
Case 'zip ':
Case 'pdf ':
Case 'rtf ':
Case 'tar ':
Return 'application/'. $ fileSuffix;
Case 'swf ':
Return 'application/x-shockwave-flash ';
Case 'flv ':
Return 'video/x-flv ';
Case 'jpe ':
Case 'jpg ':
Return 'image/jpeg ';
Case 'jpeg ':
Case 'png ':
Case 'gif ':
Case 'bmp ':
Case 'tiff ':
Return 'image/'. $ fileSuffix;
Case 'ico ':
Return 'image/vnd. microsoft. icon ';
Case 'tif ':
Return 'image/tiff ';
Case 'svg ':
Case 'svgz ':
Return 'image/svg + xml ';
Case 'rar ':
Return 'application/x-rar-compressed ';
Case 'exe ':
Case 'msi ':
Return 'application/x-msdownload ';
Case 'cab ':
Return 'application/vnd. ms-cab-compressed ';
Case 'aif ':
Return 'audio/aiff ';
Case 'mpg ':
Case 'mpe ':
Case 'mp3 ′:
Return 'audio/mpeg ';
Case 'mpeg ':
Case 'wav ':
Case 'aiff ':
Return 'audio/'. $ fileSuffix;
Case 'qt ':
Case 'mov ':
Return 'video/quicktime ';
Case 'psd ':
Return 'image/vnd. adobe. photoshop ';
Case 'ai ':
Case 'eps ':
Case 'ps ':
Return 'application/postscript ';
Case 'Doc ':
Case 'docx ':
Return 'application/msword ';
Case 'xls ':
Case 'xlt ':
Case 'xlm ':
Case 'xld ':
Case 'xla ':
Case 'xlc ':
Case 'xlw ':
Case 'xll ':
Return 'application/vnd. ms-excel ';
Case 'ppt ':
Case 'pps ':
Return 'application/vnd. ms-powerpoint ';
Case 'odt ':
Return 'application/vnd. oasis. opendocument. text ';
Case 'ods ':
Return 'application/vnd. oasis. opendocument. spreadsheet ';
Default:
If (function_exists ('mime _ content_type ')){
$ FileSuffix = mime_content_type ($ filename );
} Else {
$ FileSuffix = 'application/octet-stream ';
}
Return $ fileSuffix;
Break;
}
}
Love Sharing? Php supports code for multipart and resumable file download.
Http://www.ihref.com/read-3191.html