Domanda

Come posso dividere un file di grandi dimensioni di testo in file separati dal conteggio dei caratteri con PHP? Quindi, un file di 10.000 personaggio diviso ogni 1000 caratteri sarebbero divisi in 10 file. Inoltre, posso dividere solo dopo che un punto è trovato?

Grazie.

UPDATE 1:? Mi piace zombats codice e ho rimosso alcuni errori e sono venuto su con quanto segue, ma qualcuno sa come dividere solo dopo un punto

$i = 1;
    $fp = fopen("test.txt", "r");
    while(! feof($fp)) {
        $contents = fread($fp,1000);
        file_put_contents('new_file_'.$i.'.txt', $contents);
        $i++;
    }

UPDATE 2: Ho preso zombats suggerimento e modificato il codice a quello qui sotto e sembra funzionare -

$i = 1;
    $fp = fopen("test.txt", "r");
    while(! feof($fp)) {
        $contents = fread($fp,20000);
        $contents .= stream_get_line($fp,1000,".");
        $contents .=".";

        file_put_contents("Split/".$tname."/"."new_file_".$i.".txt", $contents);
        $i++;
    }
È stato utile?

Soluzione

Si dovrebbe essere in grado di realizzare questo facilmente con un semplice fread () . È possibile specificare il numero di byte che si desidera leggere, quindi è banale da leggere in un importo e di uscita si precisa in un nuovo file.

Provare qualcosa di simile a questo:

$i = 1;
$fp = fopen("test.txt",'r');
while(! feof($fp)) {
    $contents = fread($fp,1000);
    file_put_contents('new_file_'.$i.'.txt',$contents);
    $i++;
}

Modifica

Se si desidera arrestare dopo un certo periodo di durata o in un certo carattere, allora si potrebbe utilizzare stream_get_line () al posto di fread(). E 'quasi identico, tranne che consente di specificare qualsiasi delimitatore finale che si desidera. Si noti che non restituire il delimitatore come parte della lettura.

$contents = stream_get_line($fp,1000,".");

Altri suggerimenti

C'è un bug nella funzione di corsa; $split variabile non definita.

Il modo più semplice è quello di leggere il contenuto del file, dividere il contenuto, quindi salvare in altri due file. Se i file sono più di un paio di gigabyte, si sta andando ad avere un problema a farlo in PHP a causa di limitazioni di dimensioni interi.

Si potrebbe anche scrivere una classe per fare questo per voi.

<?php

/**
* filesplit class : Split big text files in multiple files
*
* @package
* @author Ben Yacoub Hatem <hatem@php.net>
* @copyright Copyright (c) 2004
* @version $Id$ - 29/05/2004 09:02:10 - filesplit.class.php
* @access public
**/
class filesplit{
    /**
     * Constructor
     * @access protected
     */
    function filesplit(){

    }

    /**
     * File to split
     * @access private
     * @var string
     **/
    var $_source = 'logs.txt';

    /**
     *
     * @access public
     * @return string
     **/
    function Getsource(){
        return $this->_source;
    }

    /**
     *
     * @access public
     * @return void
     **/
    function Setsource($newValue){
        $this->_source = $newValue;
    }

    /**
     * how much lines per file
     * @access private
     * @var integer
     **/
    var $_lines = 1000;

    /**
     *
     * @access public
     * @return integer
     **/
    function Getlines(){
        return $this->_lines;
    }

    /**
     *
     * @access public
     * @return void
     **/
    function Setlines($newValue){
        $this->_lines = $newValue;
    }

    /**
     * Folder to create splitted files with trail slash at end
     * @access private
     * @var string
     **/
    var $_path = 'logs/';

    /**
     *
     * @access public
     * @return string
     **/
    function Getpath(){
        return $this->_path;
    }

    /**
     *
     * @access public
     * @return void
     **/
    function Setpath($newValue){
        $this->_path = $newValue;
    }

    /**
     * Configure the class
     * @access public
     * @return void
     **/
    function configure($source = "",$path = "",$lines = ""){
        if ($source != "") {
            $this->Setsource($source);
        }
        if ($path!="") {
            $this->Setpath($path);
        }
        if ($lines!="") {
            $this->Setlines($lines);
        }
    }


    /**
     *
     * @access public
     * @return void
     **/
    function run(){
        $i=0;
        $j=1;
        $date = date("m-d-y");
        unset($buffer);

        $handle = @fopen ($this->Getsource(), "r");
        while (!feof ($handle)) {
          $buffer .= @fgets($handle, 4096);
          $i++;
              if ($i >= $split) {
              $fname = $this->Getpath()."part.$date.$j.txt";
               if (!$fhandle = @fopen($fname, 'w')) {
                    print "Cannot open file ($fname)";
                    exit;
               }

               if (!@fwrite($fhandle, $buffer)) {
                   print "Cannot write to file ($fname)";
                   exit;
               }
               fclose($fhandle);
               $j++;
               unset($buffer,$i);
                }
        }
        fclose ($handle);
    }


}
?>


Usage Example
<?php
/**
* Sample usage of the filesplit class
*
* @package filesplit
* @author Ben Yacoub Hatem <hatem@php.net>
* @copyright Copyright (c) 2004
* @version $Id$ - 29/05/2004 09:14:06 - usage.php
* @access public
**/

require_once("filesplit.class.php");

$s = new filesplit;

/*
$s->Setsource("logs.txt");
$s->Setpath("logs/");
$s->Setlines(100); //number of lines that each new file will have after the split.
*/

$s->configure("logs.txt", "logs/", 2000);
$s->run();
?>

http://www.weberdev.com/get_example-3894.html

ho avuto classe fix e lavoro perfetto con file .txt.

<?php

/**
* filesplit class : Split big text files in multiple files
*
* @package
* @author Ben Yacoub Hatem <hatem@php.net>
* @copyright Copyright (c) 2004
* @version $Id$ - 29/05/2004 09:02:10 - filesplit.class.php
* @access public
**/
class filesplit{
    /**
     * Constructor
     * @access protected
     */
    function filesplit(){

    }

    /**
     * File to split
     * @access private
     * @var string
     **/
    var $_source = 'logs.txt';

    /**
     *
     * @access public
     * @return string
     **/
    function Getsource(){
        return $this->_source;
    }

    /**
     *
     * @access public
     * @return void
     **/
    function Setsource($newValue){
        $this->_source = $newValue;
    }

    /**
     * how much lines per file
     * @access private
     * @var integer
     **/
    var $_lines = 1000;

    /**
     *
     * @access public
     * @return integer
     **/
    function Getlines(){
        return $this->_lines;
    }

    /**
     *
     * @access public
     * @return void
     **/
    function Setlines($newValue){
        $this->_lines = $newValue;
    }

    /**
     * Folder to create splitted files with trail slash at end
     * @access private
     * @var string
     **/
    var $_path = 'logs/';

    /**
     *
     * @access public
     * @return string
     **/
    function Getpath(){
        return $this->_path;
    }

    /**
     *
     * @access public
     * @return void
     **/
    function Setpath($newValue){
        $this->_path = $newValue;
    }

    /**
     * Configure the class
     * @access public
     * @return void
     **/
    function configure($source = "",$path = "",$lines = ""){
        if ($source != "") {
            $this->Setsource($source);
        }
        if ($path!="") {
            $this->Setpath($path);
        }
        if ($lines!="") {
            $this->Setlines($lines);
        }
    }


    /**
     *
     * @access public
     * @return void
     **/
    function run(){

        $buffer = '';
        $i=0;
        $j=1;
        $date = date("m-d-y");
        $handle = @fopen ($this->Getsource(), "r");

        while (!feof ($handle)) {

            $buffer .= @fgets($handle, 4096);
            $i++;

            if ($i >= $this->getLines()) { 

                // set your filename pattern here.
                $fname = $this->Getpath()."split_{$j}.txt";

                if (!$fhandle = @fopen($fname, 'w')) {
                    print "Cannot open file ($fname)";
                    exit;
                }

                if (!@fwrite($fhandle, $buffer)) {
                    print "Cannot write to file ($fname)";
                    exit;
                }
                fclose($fhandle);
                $j++;
                unset($buffer,$i);
            }
        } 
        if ( !empty($buffer) && !empty($i) ) {
            $fname = $this->Getpath()."split_{$j}.txt";

            if (!$fhandle = @fopen($fname, 'w')) {
                print "Cannot open file ($fname)";
                exit;
            }

            if (!@fwrite($fhandle, $buffer)) {
                print "Cannot write to file ($fname)";
                exit;
            }
                fclose($fhandle);
            unset($buffer,$i);  
        }
        fclose ($handle);
    }


}
?>

Utilizzo Esempio

<?php

require_once("filesplit.class.php");

$s = new filesplit;
$s->Setsource("logs.txt");
$s->Setpath("logs/");
$s->Setlines(100); //number of lines that each new file will have after the split.
//$s->configure("logs.txt", "logs/", 2000);
$s->run();
?>

Nella funzione run, ho fatto le seguenti regolazioni, per risolvere il "split non è definito" warning.

function run(){

        $buffer='';
        $i=0;
        $j=1;
        $date = date("m-d-y");
        $handle = @fopen ($this->Getsource(), "r");

        while (!feof ($handle)) {

            $buffer .= @fgets($handle, 4096);
            $i++;

            if ($i >= $this->getLines()) { // $split was here, empty value..

                // set your filename pattern here.
                $fname = $this->Getpath()."dma_map_$date.$j.csv";

                if (!$fhandle = @fopen($fname, 'w')) {
                    print "Cannot open file ($fname)";
                    exit;
                }

                if (!@fwrite($fhandle, $buffer)) {
                    print "Cannot write to file ($fname)";
                    exit;
                }
                fclose($fhandle);
                $j++;
                unset($buffer,$i);
            }
        }
        fclose ($handle);
    }

Ho usato questa classe per dividere una linea di 500.000 file CSV in 10 file, in modo phpMyAdmin può consumare senza problemi di timeout. Ha lavorato come un fascino.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top