PDA

View Full Version : create new file from old



jazzman121
October 1st, 2003, 09:25 PM
ok i have a text file... that users add text to all the time...

what i want to do is... to check everytime to see the file size and if the text file size gets greater than 50 kb

then i want to leave the first 12 lines.... but cut the rest of the text from lines 13 onwards till how manyever lines are there then create another text file (if the file doesnt exist ).. old_data.txt if it exsits jsut paste the remaning lines into that file... at the top


this is what i have so far


$fp = fopen('data.txt', 'r');

$size = 0;
if ($fp) {
while (!feof($fp))
$size += strlen(fread($fp, 4096));
fclose($fp);
if ($size >= 50000) {
flock($fp,2);
$old_f = fread($fp, filesize($file));
// i would have to enter the rest in here...
flock(fp,3);
}
}


i cant figure out the remaning part... how too go to the 13th line and copy them and paste them in another file

:( please help

jazzman121
October 2nd, 2003, 02:53 PM
anybody?? please help

Jubba
October 2nd, 2003, 03:02 PM
Well your original code is wrong. This line:


$size += strlen(fread($fp, 4096));

will never give you 50000. You specified that the max is 4096.


$size += strlen(fread($fp, filesize($file)); // it should be like that.


Anywho... I'm not really sure and I'm not on my computer to test anything so I can't give you any reliable code... You might want to try something like this tho:



$newFileText = substr($old_f, 0, 300);
$oldFileText = substr($old_f, 301, strlen($old_f));
// then create the file and write the information in.


The 300 and the 301 in that code is arbitrary... I'm not exactly sure how many characters 12 lines actually is, and like I said I don't have access to any of my resources at the moment to give you a better code...

jazzman121
October 3rd, 2003, 04:41 PM
I cant figure it out :( please help... what if my one line had more than 300 characters?