This time I will bring you a detailed explanation of the steps for cutting and merging large files with PHP. What are the precautions for cutting and merging large files with PHP? The following is a practical case, let's take a look.
Split code
split.php<?php $i = 0; //分割的块编号 $fp = fopen("hadoop.sql","rb"); //要分割的文件 $file = fopen("split_hash.txt","a"); //记录分割的信息的文本文件,实际生产环境存在redis更合适 while(!feof($fp)){ $handle = fopen("hadoop.{$i}.sql","wb"); fwrite($handle,fread($fp,5242880));//切割的块大小 5m fwrite($file,"hadoop.{$i}.sql\r\n"); fclose($handle); unset($handle); $i++; } fclose ($fp); fclose ($file); echo "ok";
Merge code
merge.php<?php $hash = file_get_contents("split_hash.txt"); //读取分割文件的信息 $list = explode("\r\n",$hash); $fp = fopen("hadoop2.sql","ab"); //合并后的文件名 foreach($list as $value){ if(!empty($value)) { $handle = fopen($value,"rb"); fwrite($fp,fread($handle,filesize($value))); fclose($handle); unset($handle); } } fclose($fp); echo "ok";
ThinkPHP framework allows page redirection method summary
PHP uses regular expressions to match provinces and cities
The above is the detailed content of Detailed explanation of steps to implement large file cutting and merging with PHP. For more information, please follow other related articles on the PHP Chinese website!