PHP脚本将大文本文件拆分为多个文件

时间:2011-02-18 09:54:41

标签: php

我正在努力创建一个PHP脚本,以帮助根据行数将大文本文件拆分为多个较小的文件。我需要增加分割的选项,所以它从第一个文件的10行,第二个文件的20行开始,依此类推。

4 个答案:

答案 0 :(得分:4)

以下是我的脚本中的一个函数:

<?php
/**
 *
 * Split large files into smaller ones
 * @param string $source Source file
 * @param string $targetpath Target directory for saving files
 * @param int $lines Number of lines to split
 * @return void
 */
function split_file($source, $targetpath='./logs/', $lines=10){
    $i=0;
    $j=1;
    $date = date("m-d-y");
    $buffer='';

    $handle = @fopen ($source, "r");
    while (!feof ($handle)) {
        $buffer .= @fgets($handle, 4096);
        $i++;
        if ($i >= $lines) {
            $fname = $targetpath.".part_".$date.$j.".log";
            if (!$fhandle = @fopen($fname, 'w')) {
                echo "Cannot open file ($fname)";
                exit;
            }

            if (!@fwrite($fhandle, $buffer)) {
                echo "Cannot write to file ($fname)";
                exit;
            }
            fclose($fhandle);
            $j++;
            $buffer='';
            $i=0;
            $line+=10; // add 10 to $lines after each iteration. Modify this line as required
        }
    }
    fclose ($handle);
}
?>

答案 1 :(得分:3)

应该是评论,但我还不能发表评论。 AndyDeGroo的回答是错误的。它不会写入最后一行文件,也不会在低于10行时保存文件。文件也应该保存在AFTER while()循环后写入文件的其余部分。固定功能应如下所示:

<?php
/**
 *
 * Split large files into smaller ones
 * @param string $source Source file
 * @param string $targetpath Target directory for saving files
 * @param int $lines Number of lines to split
 * @return void
 */
function split_file($source, $targetpath='./logs/', $lines=10){
    $i=0;
    $j=1;
    $date = date("m-d-y");
    $buffer='';

    $handle = @fopen ($source, "r");
    while (!feof ($handle)) {
        $buffer .= @fgets($handle, 4096);
        $i++;
        if ($i >= $lines) {
            $fname = $targetpath.".part_".$date.$j.".log";
            saveToFile($buffer, $fname);
            $j++;
            $i=0;
        }
    }
    $fname = $targetpath.".part_".$date.$j.".log";
    saveToFile($buffer, $fname);
    fclose ($handle);
}

function saveToFile(&$buffer, $fname)
{
    if (!$fhandle = @fopen($fname, 'w')) {
        echo "Cannot open file ($fname)";
        exit;
    }
    if (!@fwrite($fhandle, $buffer)) {
        echo "Cannot write to file ($fname)";
        exit;
    }
    fclose($fhandle);
    $buffer = '';
}
?>

PS。我删除了“$ line”变量,因为它没有在任何地方使用过。

答案 2 :(得分:0)

嗯。你不需要包含文件的最后一部分吗? 我想替换“if($ i&gt; = $ lines){” 用“if($ i&gt; = $ lines || feof($ handle)){”

答案 3 :(得分:0)

$handle = fopen('source/file/path','r'); 
        $f = 1; //new file number
        while(!feof($handle))
        {
            $newfile = fopen('new/file/path'.$f.'.txt','w'); //create new file to write to with file number
            for($i = 1; $i <= 5000; $i++) //for 5000 lines
            {
                $import = fgets($handle);
                //print_r($import);
                fwrite($newfile,$import);
                if(feof($handle))
                {break;} //If file ends, break loop
            }
            fclose($newfile);

            $f++; //Increment newfile number
        }
        fclose($handle);