Jump to content

Trying to read a text file and save it as X files all Y lines long...


Recommended Posts

Hey guys, I'm trying to open a file, and save it to however many files, with $numlines lines each. Can anyone advise me the most "efficient way to do it"

 

I am thinking of...

-reading it line by line with a counter, then saving the files into pieces.

 

Is that a efficient way of doing it? Because I will be dealing with 200k+ lines in a single text file, and I don't want to blow up my shared server, or get a "timeout" on my page. Thanks!

you're better off doing it the way you're planning on doing it, otherwise you'd be calling fread 200k+ times instead of just buffering the whole content and working it out.. BUT what I can say is.. close every file resource you create or that will set flags off on your webhosting that you'd using too much cpu time and they'll  lock your account..

on my netbook this code

<?php
$t = microtime(1);
$test = file('tolstoy.txt');//65k lines
$test = array_merge($test, $test);
$test = array_merge($test, $test);
$test = array_chunk($test, 1000);
$name = 'part';
$i = 0;
foreach ($test as $part){
$i++;
$f = fopen('part'.$i.'.txt', 'w');
$part = implode("\n",$part);
fwrite($f, $part);
fclose($f);
}
echo microtime(1)-$t," $i";
?>

output

1.6787929534912 262

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.