【问题标题】:Splitting up a large text document into multiple smaller text files将大文本文档拆分为多个较小的文本文件
【发布时间】:2025-08-05 11:35:02
【问题描述】:

我正在开发一个使用fwrite() 来编写文本的文本收集引擎,但我想在写入过程中设置 1.5 mb 的文件大小上限,因此如果文件大于 1.5mb,它将开始从它停止的地方等等,直到它将源文件的内容写入多个文件。我在 Google 上搜索过,但许多教程和示例对我来说太复杂了,因为我是一名新手程序员。下面的代码位于获取文本 ($RemoveTwo) 的 for 循环内。它不能按我的需要工作。任何帮助将不胜感激。

        switch ($FileSizeCounter) {
            case ($FileSizeCounter> 1500000):
                 $myFile2 = 'C:\TextCollector/'.'FilenameA'.'.txt';
                 $fh2 = fopen($myFile2, 'a') or die("can't open file");
                    fwrite($fh2, $RemoveTwo);
                    fclose($fh2);  
                break;
            case ($FileSizeCounter> 3000000):
                 $myFile3 = 'C:\TextCollector/'.'FilenameB'.'.txt';
                 $fh3 = fopen($myFile3, 'a') or die("can't open file");
                    fwrite($fh3, $RemoveTwo);
                    fclose($fh3);  
                break;
            default:
                echo "continue and continue until it stops by the user";
        }

【问题讨论】:

  • 您应该在第一个文件中包含之前的代码以获得更完整的答案。
  • 我补充说明@julie

标签: php if-statement for-loop switch-statement fwrite


【解决方案1】:

尝试做这样的事情。您需要从源中读取然后逐段写入,同时检查源中的文件结尾。当您比较最大值和缓冲区值时,如果它们是true,则关闭当前文件并使用自动递增的数字打开一个新文件:

/*
** @param $filename [string] This is the source
** @param $toFile [string] This is the base name for the destination file & path
** @param $chunk [num] This is the max file size based on MB so 1.5 is 1.5MB
*/
function breakDownFile($filename,$toFile,$chunk = 1)
    {
        // Take the MB value and convert it into KB
        $chunk      =   ($chunk*1024);
        // Get the file size of the source, divide by kb
        $length     =   filesize($filename)/1024;
        // Put a max in bits
        $max        =   $chunk*1000;
        // Start value for naming the files incrementally
        $i          =   1;
        // Open *for reading* the source file
        $r          =   fopen($filename,'r');
        // Create a new file for writing, use the increment value
        $w          =   fopen($toFile.$i.'.txt','w');
        // Loop through the file as long as the file is readable
        while(!feof($r)) {
            // Read file but only to the max file size value set
            $buffer =   fread($r, $max);
            // Write to disk using buffer as a guide
            fwrite($w, $buffer);
            // Check the bit size of the buffer to see if it's
            // same or larger than limit
            if(strlen($buffer) >= $max) {
                // Close the file
                fclose($w);
                // Add 1 to our $i file
                $i++;
                // Start a new file with the new name
                $w  =   fopen($toFile.$i.'.txt','w');
           }
        }
        // When done the loop, close the writeable file
        fclose($w);
        // When done loop close readable
        fclose($r);
    }

使用方法:

breakDownFile(__DIR__.'/test.txt',__DIR__.'/tofile',1.5);

【讨论】:

  • 谢谢@Rasclatt,但对我来说很难理解
  • 不太清楚该告诉你什么。没有比人类更易读的更简单的方法了。您打开一个源文件,然后启动一个目标文件,然后循环检查源文件以查看文件是否已完成读取,同时您一直在写入目标文件。一旦循环达到读取的最大文件大小,它就会关闭目标文件并从它停止的地方开始一个新文件。它递增$i 变量,因此文件以递增方式编号。它重复循环,直到完成读取文件。