当前位置: 移动技术网 > IT编程>开发语言>PHP > php断点续传之如何分割合并文件

php断点续传之如何分割合并文件

2019年03月28日  | 移动技术网IT编程  | 我要评论
复制代码 代码如下: <?php ini_set("memory_limit", "50m");//必须的,根据你环境的实际情况尽量大,防止报错 ini_set("m
复制代码 代码如下:

<?php
ini_set("memory_limit", "50m");//必须的,根据你环境的实际情况尽量大,防止报错
ini_set("max_execution_time", "100");
//file_exists() 函数检查文件或目录是否存在,存在则返回 true,否则返回 false。
//fread() 函数读取文件(可安全用于二进制文件)。fread() 从文件指针 file 读取最多 length 个字节。
//filesize() 函数返回指定文件的大小(字节)。本函数的结果会被缓存。请使用 clearstatcache() 来清除缓存。
$orgfile = 'fireworks8-chs.exe';//源文件
$cachefilename = 'vbcache';//分割成的临时文件块
function cutfile($filename,$block) {//分割
global $cachefilename;
if (!file_exists($filename)) return false;
$num = 1;
$file = fopen($filename, 'rb');
while ($content = fread($file,$block)) {
$cachefile = $cachefilename . $num++ . '.dat';
$cfile = fopen($cachefile, 'wb');
fwrite($cfile, $content);
fclose($cfile);
}
fclose($file);
}
function mergefile($targetfile) {//合并
global $cachefilename;
$num = 1;
$file = fopen($targetfile, 'wb');
while ($num > 0) {
$cachefile = $cachefilename . $num++ . '.dat';
if (file_exists($cachefile)) {
$cfile = fopen($cachefile, 'rb');
$content = fread($cfile, filesize($cachefile));
fclose($cfile);
fwrite($file, $content);
}
else {
$num = -1;
}
}
fclose($file);
}
//调用
cutfile($orgfile, 10 * pow(2,20)); //10 * pow(2,20) 就等于 10m pow() 函数返回 x 的 y 次方
mergefile('ok.exe');
?>

如您对本文有疑问或者有任何想说的,请 点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网