PHP file splitting and merging (resumable upload)

怪我咯
Release: 2023-03-13 11:04:02
Original
2810 people have browsed it

This article mainly introduces the file segmentation and merging of PHP breakpoint resume transfer in detail. It has certain reference value. Interested friends can refer to

php to implement breakpoint resume transfer. You need to split the large file into multiple small files, and then upload a single . Merge after transmission.

│ merge.php – merge file script
│ merge.zip – merged file
│ socket.zip – file that needs to be split
│ split.php – Split file script

└─split – Split small file directory

The following is the source code

split.php

<?php

$fp = fopen("socket.zip", "rb");
$filesize = 10;
$i = 0;
$no = 1;
while(!feof($fp))
{
  $file = fread($fp, $filesize);

  $fp2 = fopen("./split/socket.port".sprintf("%04d",$no).".".$i."-".($i+$filesize).".tmp", "wb");
  fwrite($fp2, $file, $filesize);
  fclose($fp2);
  $i+=$filesize+1;
$no++;
}

fclose($fp);
Copy after login

merge.php

<?php
$filelist = glob(&#39;./split/*socket*.tmp&#39;);
$filesize = 10;

//print_r($filelist);
$mergeFileName = &#39;merg.zip&#39;;

unlink($mergeFileName);
  $fp2 = fopen($mergeFileName,"w+");
foreach($filelist as $k => $v)
{
  $fp = fopen($v, "rb");
   $content = fread($fp, $filesize);

   fwrite($fp2, $content, $filesize);
   unset($content);
   fclose($fp);
   echo $k,"\n";
}
  fclose($fp2);
Copy after login

The following code introduces how to split and merge files in PHP breakpoint resume. Friends who need it can refer to it

<?php 
ini_set("memory_limit", "50M");//必须的,根据你环境的实际情况尽量大,防止报错 
ini_set("max_execution_time", "100"); 
//file_exists() 函数检查文件或目录是否存在,存在则返回 true,否则返回 false。 
//fread() 函数读取文件(可安全用于二进制文件)。fread() 从文件指针 file 读取最多 length 个字节。 
//filesize() 函数返回指定文件的大小(字节)。本函数的结果会被缓存。请使用 clearstatcache() 来清除缓存。 
$orgFile = &#39;Fireworks8-chs.exe&#39;;//源文件 
$cacheFileName = &#39;vbcache&#39;;//分割成的临时文件块 
function cutFile($fileName,$block) {//分割 
global $cacheFileName; 
if (!file_exists($fileName)) return false; 
$num = 1; 
$file = fopen($fileName, &#39;rb&#39;); 
while ($content = fread($file,$block)) { 
$cacheFile = $cacheFileName . $num++ . &#39;.dat&#39;; 
$cfile = fopen($cacheFile, &#39;wb&#39;); 
fwrite($cfile, $content); 
fclose($cfile); 
} 
fclose($file); 
} 
function mergeFile($targetFile) {//合并 
global $cacheFileName; 
$num = 1; 
$file = fopen($targetFile, &#39;wb&#39;); 
while ($num > 0) { 
$cacheFile = $cacheFileName . $num++ . &#39;.dat&#39;; 
if (file_exists($cacheFile)) { 
$cfile = fopen($cacheFile, &#39;rb&#39;); 
$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(&#39;ok.exe&#39;); 
?>
Copy after login

The above is the detailed content of PHP file splitting and merging (resumable upload). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!