File Splitter dilemma

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
rajkumar_pb
Forum Newbie
Posts: 6
Joined: Mon Oct 26, 2009 1:45 am

File Splitter dilemma

Post by rajkumar_pb »

Guys got this code over the net, which is used to split a large file into a chunk of small files. But the problem, it has supposed to split the file once eve 300lines, but its not. it split single line and store it as a file. Say if my text file has 4000 lines, my folder filled up with 4000 text files. Please help me come over this problem. I need to split it as files of 300 lines.

Heres the code

Code: Select all

<?php
class filesplit{
    /**
     * Constructor
     * @access protected
     */
    function filesplit(){
       
    }
   
    /**
     * File to split
     * @access private
     * @var string
     **/
    var $_source = 'file_to_split.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 = 300; [color=#FF4000]// No matter what value i changed here, it doesnt work...[/color]
   
    /**
     *
     * @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);
    } 
    }
?>
 
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: File Splitter dilemma

Post by requinix »

Line 121.

Code: Select all

if ($i >= $split)
Should say

Code: Select all

if ($i >= $this->_lines)
Lack of highlighting is intentional.
rajkumar_pb
Forum Newbie
Posts: 6
Joined: Mon Oct 26, 2009 1:45 am

Re: File Splitter dilemma

Post by rajkumar_pb »

tasairis wrote:Line 121.

Code: Select all

if ($i >= $split)
Should say

Code: Select all

if ($i >= $this->_lines)
Lack of highlighting is intentional.
Well thats working. But if i have a 477 records and wanna split them as part of 250, then it split the 1st 250 and leave the rest. No matter how many lines rest around, i need them in a text file too. Will you got any suggestion for that?
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: File Splitter dilemma

Post by requinix »

Hmm. Quite.

Code: Select all

if ($i >= $this->_lines || feof($handle))
rajkumar_pb
Forum Newbie
Posts: 6
Joined: Mon Oct 26, 2009 1:45 am

Re: File Splitter dilemma

Post by rajkumar_pb »

tasairis wrote:Hmm. Quite.

Code: Select all

if ($i >= $this->_lines || feof($handle))

^^ :oops: How silly i am.. :crazy:

Thanks man, you're awesome... :drunk:
Post Reply