Jump to content

Using fopen to extract 500 lines


holyearth

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).
Link to comment
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
Link to comment
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
Link to comment
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]
Link to comment
Share on other sites

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.