Jump to content

Archived

This topic is now archived and is closed to further replies.

holyearth

Using fopen to extract 500 lines

Recommended Posts

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).

Share this post


Link to post
Share on other sites
You're description is a little muddy. What do you mean by "saving the first 500 lines as $startnum"?
Use the [url=http://www.php.net/file]file()[/url] 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".

[code]<?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);
}
?>[/code]

BTW, please don't bump so quickly.

Ken

Share this post


Link to post
Share on other sites
I think this is what you want.
[code]<?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);
}
?>[/code]

Ray

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
You have to loop through the array, echoing each line:
[code]<?php
foreach($startnum as $line)
    echo nl2br($line);
?>[/code]

Ken

Share this post


Link to post
Share on other sites
If you are using the code I gave you then add this line.

[code]<?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>";
}
?>[/code]

Share this post


Link to post
Share on other sites

×

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.