Jump to content


Photo

Using fopen to extract 500 lines


  • Please log in to reply
9 replies to this topic

#1 holyearth

holyearth
  • Members
  • PipPip
  • Member
  • 23 posts

Posted 19 October 2006 - 05:11 PM

Hello everyone, need some help...

I have the following code:

$process = fopen("hitcounter.txt", "r"); //opens txt file for startnum
$startnum = fread($process,8192); //starting number

Which opens hitcounter.txt and extracts data from it and saves it as $startnum

How can I make it extract the data (500 lines at a time) and DELETE the data it extracted.

So, it will open hitcounter.txt pull the first 500 lines save it as $startnum and delete the 500 lines from hitcounter.txt (the ones it extracted).

#2 holyearth

holyearth
  • Members
  • PipPip
  • Member
  • 23 posts

Posted 19 October 2006 - 06:31 PM

*Bump*

Anyone?

#3 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 19 October 2006 - 06:41 PM

You're description is a little muddy. What do you mean by "saving the first 500 lines as $startnum"?
Use the file() function to read the whole file into an array. Start at entry 500 (which skips the first 500 lines) and write the rest back to "hitcounter.txt".

<?php
$hc_file = file('hitcounter.txt');
if (count($hc_file) < 500) echo "Less than 500 lines in the files";
else {
    $fp = fopen('hitcounter.txt','w'); // open a new version of the file for write
    for ($i=500;$i<count($hc_file);$i++)
         fwrite($fp,$hc_file[$i]);
    fclose($fp);
}
?>

BTW, please don't bump so quickly.

Ken

#4 craygo

craygo
  • Staff Alumni
  • Advanced Member
  • 1,973 posts
  • LocationRhode Island

Posted 19 October 2006 - 07:10 PM

I think this is what you want.
<?php
$file = "hitcounter.txt";
$textfile = file($file);
$total_lines = count($textfile);
$lines = 500; // set the number of lines you want to use here
if($total_lines < $lines){
echo "Less than $lines in this file";
} else {
// here are your first 500 lines put into an array called $startnum. can do what you like with them
$startnum = array();
for($first=0; $first<$lines; $first++){
$startnum[] = $textfile[$first];
}
// Here we store the rest of the file into an array for re-writing
$handle = fopen($file, "w");
for($last=$lines; $last<$total_lines; $last++){
fwrite($handle, $textfile[$last]);
}
fclose($handle);
}
?>

Ray

#5 holyearth

holyearth
  • Members
  • PipPip
  • Member
  • 23 posts

Posted 19 October 2006 - 07:43 PM

I'm trying to open hitcounter.txt "pull" the first 500 lines as $something

BUT

I want the 500 lines that were "pulled" from hitcounter.txt to be "removed" while they are pulled.

So, it should extract the 500 lines and save the file without those 500 lines.

#6 craygo

craygo
  • Staff Alumni
  • Advanced Member
  • 1,973 posts
  • LocationRhode Island

Posted 19 October 2006 - 07:49 PM

that's what the above code does.

it takes the first 500 lines and stores them as $startnum which is an array. then it takes the rest of the data and writes it back without the first 500 lines.

Ray

#7 holyearth

holyearth
  • Members
  • PipPip
  • Member
  • 23 posts

Posted 19 October 2006 - 08:03 PM

Ray,

Got it thanks!

Now, im trying to "output"  (to the user)  the 500 lines it saved as an array using:

<? echo("$startnum"); ?>

But it only shows: Array

#8 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 19 October 2006 - 08:07 PM

You have to loop through the array, echoing each line:
<?php
foreach($startnum as $line)
     echo nl2br($line);
?>

Ken

#9 craygo

craygo
  • Staff Alumni
  • Advanced Member
  • 1,973 posts
  • LocationRhode Island

Posted 19 October 2006 - 08:12 PM

If you are using the code I gave you then add this line.

<?php
// here are your first 500 lines put into an array called $startnum. can do what you like with them
$startnum = array();
for($first=0; $first<$lines; $first++){
$startnum[] = $textfile[$first];
echo $textfile[$first]."<br>";
}
?>


#10 holyearth

holyearth
  • Members
  • PipPip
  • Member
  • 23 posts

Posted 19 October 2006 - 08:26 PM

Thank you ken and craygo.

I appreciate your time and help! God Bless!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users