writer, web developer, designer

From the Blog

May
30

PHP file splitting

Posted by Shell on May 30th, 2011 at 4:09 pm

Little code snippit that I needed today that splits a text file line by line into sequentially numbered single-line text files: 

<?php
   $file = fopen("data.txt", "r");
   $x = 0;
   while ( !feof( $file ) ) {
      $fn = $x . ".txt";
      $newfile = fopen( $fn, "w" );
      $data = fgets($file);
      fwrite( $newfile, $data );
      fclose( $newfile );
      $x++;
   }
   fclose( $file );
?>
									

Leave a Reply

  1.  

    |