php断点续传时如何把大文件分割的源码分享

php实现断点续传,就需要把大文件分割成多个小文件,然后单个上传。传完后在合并。

  1. │ merge.php –合并文件脚本
  2. │ merge.zip –合并后文件
  3. │ socket.zip –需要分割的文件
  4. │ split.php –分割文件脚本
  5. └─split –分割后小文件目录

下面是源码,split.php

  1. <?php
  2. $fp=fopen("socket.zip","rb");
  3. $filesize= 10;
  4. $i= 0;
  5. $no= 1;
  6. while(!feof($fp))
  7. {
  8. $file=fread($fp,$filesize);
  9. $fp2=fopen("./split/socket.port".sprintf("%04d",$no).".".$i."-".($i+$filesize).".tmp","wb");
  10. fwrite($fp2,$file,$filesize);
  11. fclose($fp2);
  12. $i+=$filesize+1;
  13. $no++;
  14. }
  15. fclose($fp);
  16. merge.php
  17. <?php
  18. $filelist=glob('./split/*socket*.tmp');
  19. $filesize= 10;
  20. //print_r($filelist);
  21. $mergeFileName='merg.zip';
  22. unlink($mergeFileName);
  23. $fp2=fopen($mergeFileName,"w+");
  24. foreach($filelistas$k=>$v)
  25. {
  26. $fp=fopen($v,"rb");
  27. $content=fread($fp,$filesize);
  28. fwrite($fp2,$content,$filesize);
  29. unset($content);
  30. fclose($fp);
  31. echo$k,"\n";
  32. }
  33. fclose($fp2);