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

简介:

wKioL1L7MQSTFjsnAAKa3CEUI5Q764.jpg


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
<?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' );
?>


源码下载:http://down.51cto.com/data/1081444



最近在研究php断点续传上传文件(要求不能使用swf插件),算是有点进展吧!

思路01:将文件分割成若干部分,后台一个一个接收合并,并记录下当前合并成功到了第几个。下此从这个开始合并,直到合并完成。判断:生成文件的大小和文件md5码是否一致。返回true or false。


思路02:文件流。通过记录指针写入文件。记录这次写到了第几针,下次从这里开始写。




继续研究中......







      本文转自许琴 51CTO博客,原文链接:http://blog.51cto.com/xuqin/1358490,如需转载请自行联系原作者




相关文章
|
1月前
thinkphp5.1隐藏index.php入口文件
thinkphp5.1隐藏index.php入口文件
28 0
thinkphp5.1隐藏index.php入口文件
|
3月前
|
PHP 数据安全/隐私保护
php案例:判断这个文件是什么编程语言代码的文件(判断java或者php)
php案例:判断这个文件是什么编程语言代码的文件(判断java或者php)
php案例:判断这个文件是什么编程语言代码的文件(判断java或者php)
php案例:用代码的方式创建目录+文件+写入数据(都由你定)
php案例:用代码的方式创建目录+文件+写入数据(都由你定)
php案例:用代码的方式创建目录+文件+写入数据(都由你定)