Splitting a file based on size

Discussion in 'PHP' started by protocol96, Sep 28, 2007.

  1. #1
    has some1 written a script to break a text file into multiple files. I have to check if the size of the text, if it greater than 4KB i need to split it into different file each not greater than 4KB
     
    protocol96, Sep 28, 2007 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    This does the trick.
    
    <?php
    
    $file = 'longtext.txt';
    
    $content = file_get_contents($file);
    $chunks = 1;
    
    if (strlen($content) > 4096)
    {
    	foreach (str_split($content, 4096) AS $chunk)
    	{
    		file_put_contents("chunk_{$chunks}.txt", $chunk);	
    		$chunks++;
    	}
    }
    
    ?>
    
    PHP:
     
    nico_swd, Sep 28, 2007 IP
  3. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #3
    I edited my post above.
     
    nico_swd, Sep 28, 2007 IP
  4. protocol96

    protocol96 Peon

    Messages:
    413
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #4
    wat will i do without you, man!!

    yea, so finally this was my last day and last code as a coder. Thanks a lot nico, u saved my butt lot many times.

    c ya around
     
    protocol96, Sep 28, 2007 IP
  5. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #5
    Glad I could help. :)

    You should stick to coding though. It's fun. :D

    Anyway... good luck.
     
    nico_swd, Sep 28, 2007 IP