Home  >  Article  >  Backend Development  >  Example explanation of large file cutting and merging functions implemented in PHP

Example explanation of large file cutting and merging functions implemented in PHP

jacklove
jackloveOriginal
2018-06-27 17:57:351943browse

This article mainly introduces the large file cutting and merging functions implemented by PHP, involving PHP's related operating skills for file reading and writing, string traversal, segmentation, etc. Friends in need can refer to this article

The example describes the large file cutting and merging functions implemented by PHP. Share it with everyone for your reference, the details are as follows:

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";

Articles you may be interested in:

Explanation of simple word grouping algorithm implemented in PHP

mongoDB implemented in PHP Complete example explanation of database operation class

ThinkPHP framework uses redirect to implement page redirection method example explanation

The above is the detailed content of Example explanation of large file cutting and merging functions implemented in PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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