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

yipeiwu_com6年前PHP代码库
复制代码 代码如下:

<?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');
?>

相关文章

php查看session内容的函数

如:name|s:4:"tasm";passwd|s:6:"111111";mode|s:1:"1",也知道该session存放的位置,而且可以上传文件,所以嘛,当时就做了一次小小的黑客...

php生成图片缩略图的方法

本文实例讲述了php生成图片缩略图的方法。分享给大家供大家参考。具体如下: 这里需要用到GD2 library function make_thumb($src,$dest,$des...

PHP采用get获取url汉字出现乱码的解决方法

本文实例讲述了PHP采用get获取url汉字出现乱码的解决方法。分享给大家供大家参考。具体方法如下: 一、问题: 本来打算这样使用 复制代码 代码如下:<a href="list....

php tpl模板引擎定义与使用示例

本文实例讲述了php tpl模板引擎定义与使用。分享给大家供大家参考,具体如下: tpl.php <?php namespace tpl; /** * Class Tp...

php中判断数组相等的方法以及数组运算符介绍

php中判断数组相等的方法以及数组运算符介绍

如何判断两个数组相等呢?其实很简单,用 == 或者 === 就可以了 php手册里说明如下: 那像 array('k'=>array())这样的多维数组能用如上方法判断相等吗?当...