本文通过实例代码给大家介绍php下载文件的函数相关知识,介绍的非常详解,具有参考借鉴价值,感兴趣的朋友一起学习吧
通过函数完成下载文件的PHP功能代码 function download($url, $filename) {
// 获得文件大小, 防止超过2G的文件, 用sprintf来读 $filesize = sprintf ( \ if (! $filesize) { return; }
header ( \ header ( \
header ( \ header ( 'Content-transfer-encoding: binary' );
if ($range = getenv ( 'HTTP_RANGE' )) { // 当有偏移量的时候,采用206的断点续传头 $range = explode ( '=', $range ); $range = $range [1];
header ( \
header ( \
header ( \ header ( \
header ( \
header ( \ header ( \ else {
header ( \ $range = 0; } loadFile ( $url );}
function loadFile($filename, $retbytes = true) {
$buffer = ''; $cnt = 0; $handle = fopen ( $filename, 'rb' ); if ($handle === false) { return false; } while ( ! feof ( $handle ) ) {
$buffer = fread ( $handle, 1024 * 1024 ); 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;}
输入2个参数即可完成下载 download($url, $filename)
以上所述是小编给大家介绍的PHP下载文件的函数实例代码的相关知识,希望对大家
有所帮助