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

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

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

下面是源码:

split.php

  1. $fp=fopen("socket.zip","rb");
  2. $filesize= 10;
  3. $i= 0;
  4. $no= 1;
  5. while(!feof($fp))
  6. {
  7. $file=fread($fp,$filesize);
  8. $fp2=fopen("./split/socket.port".sprintf("%04d",$no).".".$i."-".($i+$filesize).".tmp","wb");
  9. fwrite($fp2,$file,$filesize);
  10. fclose($fp2);
  11. $i+=$filesize+1;
  12. $no++;
  13. }
  14. fclose($fp);

merge.php

  1. $filelist=glob('./split/*socket*.tmp');
  2. $filesize= 10;
  3. //print_r($filelist);
  4. $mergeFileName='merg.zip';
  5. unlink($mergeFileName);
  6. $fp2=fopen($mergeFileName,"w+");
  7. foreach($filelistas$k=>$v)
  8. {
  9. $fp=fopen($v,"rb");
  10. $content=fread($fp,$filesize);
  11. fwrite($fp2,$content,$filesize);
  12. unset($content);
  13. fclose($fp);
  14. echo$k,"\n";
  15. }
  16. fclose($fp2);