PHP: File writing issue - php

I'm trying to make a function that writes a list of scores for players.
For example:
player_1 100 12 12 10
player_2 39 13 48 29
And when players beat (or do worse) than their previous scores, their score is over-written with the new score.
I've written a function that sort of works, but has multiple problems.
function write($player)
{
global $logfile;
$lines = file($logfile);
foreach($lines as $i => $line)
{
$pieces = explode(" ", $line);
$pieces[0] = trim($pieces[0]);
if( $pieces[0] == $player->name ) //found name
{
trim($lines[$i]);
unset($lines[$i]); //remove the old player data
$lines[$i] = "{$player->name} {$player->lvl} {$player->exp} {$player->mana} \n"; //write the new score
$fp = fopen($logfile,'a');
fwrite($fp,$lines[$i]);
$found = TRUE;
break;
}
}
if(!$found) //record a new player whose score isn't in the file
{
$fp = fopen($logfile,'a');
$newp = "$player->name $player->lvl $player->exp $player->mana \n";
fwrite($fp, $newp);
}
fclose($fp);
}
The file just appends the new score and doesn't overwrite the previous score. Could someone point out my errors?

Try changing:
$fp = fopen($logfile,'w');
into
$fp = fopen($logfile,'a');
in your
if( $pieces[0] == $player->name ) ...
PHP.fopen file open modes ;)
EDIT
You can override your player entry by putting the fwrite() after foreach loop by overriding the whole file with joined lines (this may cause performace issues).
Or
Try to loop line by line using fgets() and then if you will find the right match use fseek() to the previous line and override it ;)
fgets() fseek()
SECOND EDIT
<?php
$find = 'player_1';
$h = fopen('play.txt','r+');
$prev_pos = 0;
while(($line = fgets($h, 4096)) !== false){
$parts = explode(' ', $line);
if($parts[0] == $find) {
fseek($h, $prev_pos);
fwrite($h, "player_222 12 22 411");
break;
}
$prev_pos = ftell($h);
}
fclose($h);
?>
Code sample as requested ;) The idea is to save previous line position and then use it to fseek and override. I'm not sure if the fwrite will work well on all enviroments without PHP_EOL at the end-of-line, but on mine it's fine.

First, let us see the reason why it duplicates the record. $lines is an array in which you are updating the record of the specific player. But after updating the record, you are appending it to the file (using "a" mode) and therefore duplicating the entry of that player.
The idea should be to update that record in the file. And with your logic, the best thing is to rewrite $lines to the file. Since $lines will always contain the updated entry, it makes sense.
Now coming to the logic where you are making an entry for a new player. There is nothing wrong in that logic but it could be improved by appending the new entry to $lines instead of writing to the file.
Here is the updated code. Please note that I've removed lines that weren't needed.
function write($player) {
global $logfile;
$found = FALSE;
$lines = file($logfile);
foreach($lines as $i => $line) {
$pieces = explode(" ", $line);
$pieces[0] = trim($pieces[0]);
if( $pieces[0] == $player->name ) { //found name
$lines[$i] = "{$player->name} {$player->lvl} {$player->exp} {$player->mana} \n"; //write the new score
$found = TRUE;
break;
}
}
if(!$found) { //record a new player whose score isn't in the file
$lines[] = "$player->name $player->lvl $player->exp $player->mana \n";
}
file_put_contents($logfile, $lines);
}
Hope it helps!

Is this code run on a web server with many users accessing at the same time?
If it is, imagine what happens when one user has just opened the file for writing, the file is emptied, and another opens it for reading before the first one has finished writing the data.
A partial solution is write to a temp file and rename the temp as the original when you are done. Rename is atomic, so the users will see either the original file or the new one and not something in between.
But youll still miss some updates. You could lock the file, meaning that when one person is writing another can't read. To do that you would use the flock function: http://php.net/manual/en/function.flock.php
The proper solution is using a real database. Sqlite for example is nice and simple: no external server processes or passwords...

Related

best way to remove items from a list php

I am trying to delete items inside List A from list B
And the two inside a text file
Example : a.txt
1
3
6
b.txt
2
3
6
I tried more than one method previously but with large files it does not work as it should
$a = file('a.txt', FILE_IGNORE_NEW_LINES);
$b = file('b.txt', FILE_IGNORE_NEW_LINES);
$n = 'new.txt';
for ($i = 0;$i < count($b);$i++)
{
if (!in_array($b[$i], $a))
{
$c = file_get_contents($n);
$c .= $b[$i] . "\n";
file_put_contents($n, $c);
}
}
Is there a better way to handle large files like 80k line?
This code mainly changes the way the files are read and written, so that the second file is read 1 line at a time and not read all in memory. The output also uses FILE_APPEND in file_put_contents() so that it doesn't need to read the file again.
The first part is to create an array of the a.txt file, with the value as the index to allow you to use isset() rather than in_array() which will make the searching a lot quicker.
Then read the second file 1 line at a time, check if it's present and add the data if needed...
$fileA = fopen('a.txt', 'r');
$a = [];
while($entry = fgets($fileA))
{
$a[trim($entry)] = true;
}
$fileB = fopen('b.txt', 'r');
$n = 'new.txt';
// Clear the file
file_put_contents($n, '');
while($b = fgets($fileB))
{
if (!isset($a[trim($b)]))
{
file_put_contents($n, $b, FILE_APPEND);
}
}

Read from a text file and post the first 50 lines into another text file?

I have a text file and it is called 'Store.txt'.
I would like to know how I can read from this file and then grab the first 50 lines of numbers/text
and insert them into another text file.
I have little code because I'm not exactly sure how to go about it and I've been searching online but couldn't find much I believe an if statment is the answer?
Any way I have gave it ago but sadly it hasn't worked.
Here is how I got on-
<?php
$fileToOpen = fopen('Store.txt', 'r');
$return = '';
$count = 0;
$return. = $fileToOpen. "\n";
if ($count >= 50)
break;
}
file_put_contents($return, "Store2nd.txt");
fclose($fileToOpen);
?>
Thank you in advance for any help. (:
This will copy upto the first 50 lines without reading in the complete file:
<?php
$fileToOpen = fopen('Store.txt', 'r');
$outputFile = fopen('Store2nd.txt', 'w');
$count = 0;
while (!feof($fileToOpen)) { // We'll copy the whole file ...
if ($count++ >= 50) // ... or the first 50 lines, whichever is less
break;
$line = fgets($fileToOpen);
fwrite($outputFile, $line);
}
fclose($fileToOpen);
fclose($outputFile);
?>
Please give this a try:
<?php
$lines = file('Store.txt'); // make file content an array
$result = array_slice($lines,0,50); // take the first 50 lines
file_put_contents('Store2nd.txt', implode('', $result)); // output
?>
A better way would probably be to do a foreach loop for your text.
Then add $count++ in your loop so that the $count = 0; you've set up will increase.
Right now with your code, nothing is increasing, so $count never reaches 50.
Cheers.

PHP Performing action on a variable line by line

I have a variable containing an html code and I want to perform an action of every line of the code:
<?php
$html = file_get_contents('http://www.google.com/');
#how i would like it to be (not real commands)
/*
$line = 1
$currentline = readline($html,$line)
line++
where $html is the variable that we want to read a line from
$line is the line number
and $currentline the contents
so if $line is 1 we get that $currentline is <html> or whatever
after that i perform things on that line and continue reading each line untill i read all the lines
*/
?>
I hope you understand what I mean/need. I’m very new to web based programming so I need a lot of explanation!
Please don’t use technical terms you think a PHP programmer should know because i do probably don't know what that is.
Your question is similar to this one although one might argue about whether the answers are understandable for a beginner. This should do what you're looking for:
<?php
$html = file_get_contents('http://www.google.com/');
// Skipped: Error checks
// Split $html into lines
$lines = explode("\n", $html);
// Iterate over all lines
foreach($lines as $line) {
// Process $line
}
// Or get the 5th line
$fifth_line = $lines[4];
// Or iterate using an index
for($line = 0; $line < count($lines); $line++) {
$theline = $lines[$line];
}

Reading only the last 10 lines of a text file "tail" (Jquery/Ajax/javascript or whatever)

I've been searching for solutions here for a long time now, and i haven't been able to find any so far
what i need is a way to read the last 10 or so lines of a text file, which continually updates every second
i've found some possible codes in php, but then i wont be able to update the file every second.
how do i get only the last 10 lines of a text file using some sort of javascript?
Try using array_slice, which will return a part of an array. In this case you want it to return the last 15 lines of the array,
$filearr = file("filename");
$lastlines = array_slice($filearr,-10);
You can change -10 (-10,-15 any value you want).
Hope it works!
Hmmm... maybe so, for large files:
$fileName = '[path_to_file]';
$file = new \SplFileObject($fileName);
$file->seek($file->getSize());
$lines = $file->key() - 1;
$file->setFlags(\SplFileObject::READ_CSV);
$counter = 0;
$records = 100;
$seek = $lines - $records;
$file->seek($seek);
$return = array();
while ($counter++ < $records) {
try {
$line = $file->fgets();
$return[] = $line;
} catch (\Exception $e) {
}
}
Didn't find the correct answer here, so i started a new question where i'm more specific with what i want
Reading ONLY the last 50 lines of a large text file

PHP Grab last 15 lines in txt file

Thank you for taking the time to read this and I will appreciate every single response no mater the quality of content. :)
Using PHP, I'm trying to get the last 15 lines of a text document (.txt) and store that data into a php variable. I understand that this is possible, however when I do get the last 15 lines, is it possible to retain the order? For example:
text document:
A
B
C
When I grab the text document from the last 15 characters, I don't want the echo to end up like:
C
B
A
All assistance is appreciated and I look forward to your replies; thank you. :) If I didn't explain anything clearly and/or you'd like me to explain in more detail, please reply. :)
Thank you.
Try using array_slice, which will return a part of an array. In this case you want it to return the last 15 lines of the array, so:
$filearray = file("filename");
$lastfifteenlines = array_slice($filearray,-15);
If you don't mind loading the entire file into memory:
$lines = array_slice(file('test.txt'), -15);
print_r($lines );
If the file is too large to fit into memory you can use a circular method:
// Read the last $num lines from stream $fp
function read_last_lines($fp, $num)
{
$idx = 0;
$lines = array();
while(($line = fgets($fp)))
{
$lines[$idx] = $line;
$idx = ($idx + 1) % $num;
}
$p1 = array_slice($lines, $idx);
$p2 = array_slice($lines, 0, $idx);
$ordered_lines = array_merge($p1, $p2);
return $ordered_lines;
}
// Open the file and read the last 15 lines
$fp = fopen('test.txt', 'r');
$lines = read_last_lines($fp, 15);
fclose($fp);
// Output array
print_r($lines);
This method will also work if the file has less than 15 lines- returning an array with however many lines are in the file.
You can use fseek with a negative position to seek backwards through the file, counting newlines as you go.
I'm too tired to write up copy/past-able code, but there are some examples in the comments to the manual page for fseek that are very close to what you want.
If the file isn't bigger than available memory you can do this:
$fArray = file("filename");
$len = sizeof($fArray);
for($i=$len -15;$i<$len ;$i++)
{
echo $fArray[$i];
}
If you have a file that is hundreds of megabytes :
$rc = fopen("file","r");
for ($i=0; $line = fgets($file) ;$i++)
{
if ($i%15 == 0)
{
$last15 = array();
}
$last15[] = $line;
}
echo join("\n",$last15);
the longer array solution:
array_slice(explode("\n",file_get_contents($file)),-15);
the shorter array solution:
array_slice(file($file),-15);
This code will open the file, show the total lines, show the header of file and show the last lines of file defined in $limit.
<?php
// open the file in read mode
$file = new SplFileObject('file.csv', 'r');
// get the total lines
$file->seek(PHP_INT_MAX);
$last_line = $file->key();
echo $last_line;
echo "<br>";
// Rewind to first line to get header
$file->rewind();
// Output first line if you need use the header to make something
echo $file->current();
echo "<br>";
// selecting the limit
$limit = 6;
// selecting the last lines using the $limit
$lines = new LimitIterator($file, $last_line - $limit, $last_line);
//print all the last 6 lines array
//print_r(iterator_to_array($lines));
//echo "<br>";
// Loop over whole file to use a single line
foreach ($lines as $line) {
print_r($line);
echo "<br>";
}

Categories