How to write the function code of downloading files that support block and breakpoint continuation in php
This article mainly introduces "how to write the download function code of php support block and breakpoint continuation file". In the daily operation, it is believed that many people have doubts about how to write the function code of php support block support and breakpoint continuation file download. The editor consulted all kinds of materials and sorted out a simple and useful operation method. Hope to answer the question "how to write php support block and breakpoint continuation file download function code" is helpful! Next, please follow the editor to study!
The copy code is as follows:
$dowmFile = dirname (_ _ FILE__). '/ Nokia-Always Here.mp3'; / / the file to be downloaded, 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] *) $/ SERVER, $_ 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) {
/ / download in parts
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 location to the end
If ($start > = $fsize | | $start
< 0) { $start = 0; $end = $fsize; } else { $end = $fsize - $start; } } else if (! $isset_start && $isset_end) { //最后n个字节 $end = $end >$fsize? $fsize: $end
$start = $fsize-$end
} else {
$start = 0
$end = $fsize
}
}
If ($isRange) {
Fseek ($fp, $start)
Header ('HTTP/1.1 206Partial 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 'avi':
Return 'video/msvideo'
Case 'wmv':
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
}
}
At this point, the study on "how to write php support block and breakpoint continuation file download function code" is over. I hope to be able to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!