I have a file that updates itself by appending, however this file becomes huge (500MB). I would like to read the last 50 lines in the file. How can this be done?
Tail -n50 will return the last 50 lines of the file.
$filename = 'test.html';
$output = shell_exec('exec tail -n50 ' . $filename);
echo $output;
Therefore, you don't have to load the entire file in memory.
Edit:
If you want to echo "<br>" after each line you do:
echo str_replace(PHP_EOL, '<br />', $output);
You'll need to use fseek to move the file pointer a certain number of bytes from the end of the file:
$fp = fopen('myfile','r');
fseek($fp,-1024, SEEK_END);
$last_kb_of_file = fgets($fp,1024);
You'll have to tell fgets how many bytes you want to read, not how many lines. It has no idea what the format of the file is. You'll have to split the result on a newline and see if you have 50 lines.
Related
i'm trying to make visitors count without sql with simple php code
counting the date list
i have log file called visits.log contains
2018-06-28
2018-06-28
only two lines
when i try to echo the lines gives me 5 i donno how but when i try to replace the log with something like
test
test
gives me 2 visitors
note: the text file doesn't contain empty space lines.
my php code
<p class="">Views today</p>
<span class=""><?php
$file="../../visitors.log";
$linecount = 0;
$handle = fopen($file, "r");
while(!feof($handle)){
$line = fgets($handle);
$linecount++;
}
fclose($handle);
echo $linecount-1;
?></span>
my adding lines code
$line = date('Y-m-d') . " - $_SERVER[REMOTE_ADDR]";
file_put_contents('visitors.log', $line . PHP_EOL, FILE_APPEND);
Try the below code to get number of lines
$no_of_lines = count(file($file));
thanks for help guys my script was adding a hidden empty lines and i deal with it with replace()
replacing the empty lines to remove it
I started learning PHP this week and need to add a comma to the end of every line in a text file. I don't really know where to start. What would I do after I load the file? I can't find any tutorial saying how to write to the end of every line only the beginning of the file or end?? Is it possible to specify the end of a every line?
What I have so far:
fopen("file.txt",a)
????
I'm using "a" because I want to preserve the data.
This should work for you:
(Here I just get all file lines with file(). Then I go through each line with array_map() and concatenate a comma at the end. After this I save the file with the new content with file_put_contents())
<?php
$lines = file("test.txt", FILE_IGNORE_NEW_LINES);
$lines = array_map(function($v){return $v . "," . PHP_EOL;}, $lines);
file_put_contents("test.txt", $lines);
?>
Example file:
a
a
a
after the script:
a,
a,
a,
EDIT:
If you don't want to add a comma to an empty line just add a condition to check if the line is empty or not like this:
<?php
$lines = file("test.txt", FILE_IGNORE_NEW_LINES);
$lines = array_map(function($v){return $v . (empty($v)?"":",") . PHP_EOL;}, $lines);
file_put_contents("test.txt", $lines);
?>
Do you want to replace all the EOL with some specific character ?
$EOLChar=",";
$myFile = fopen("file.txt", "r") ;
$newFile="";
while(!feof($myFile)) {
$newFile.= str_replace(PHP_EOL, $EOLChar, fgets($myFile));
}
fclose($myFile);
file_put_contents("file.txt", $newFile);
$size = intval(trim(fgets($fp,4)));
$triangle = range(1,$size);
for($j=0;$j<$size;$j=$j+1)
$triangle[$j] = split(" ",trim(fgets($fp,400)));
This code reads in the number of lines to read, then reads them one by one. Issue is, when first input line ends in space, it reads that space as a new line.
you can read full file content by file_get_contents.
<?php
$content= file_get_contents('myfile.txt');
echo $content;
?>
I'm writing simple function witch will read data from myfile.txt with fgets().
Content of file is something like:
1
2
3
4
Function to get first value (1):
$f = fopen ("myfile.txt", "r");
$mystring = fgets ($f);
Now, when I use $mystring to write in file like:
$f1 = fopen ("myfile2.txt", "w");
fwrite($f1, 'text' . $mystrin . 'more text');
text 'more text' goes to new row.
I want to have it in in same row with other text.
When reading a file using fgets() it will include the newline at the end. You only need to remove it.
$mystring = trim ($mystring);
This will remove any leading and trailing whitespaces.
$mystring = rtrim ($mystring, PHP_EOL);
Will remove any trailing newlines.
I have written some small code to read in a flat text file and display it. My issue is that one of the fields has alot of text in it and also some line returns. My code is using the line return is a delimiter and moving onto the next record.
How can I tell it to only split by the delimiter and ignore line returns?
Sample code I am using:
$delimiter = chr(1);
$fp = fopen('app2','r');
if (!$fp) {echo 'ERROR: Unable to open file.</table></body></html>'; exit;}
$loop = 0;
while (!feof($fp)) {
$loop++;
$line = fgets($fp,2048); //use 2048 if very long lines
$field[$loop] = explode ($delimiter, $line);
echo '
<tr>
<td>'.$field[$loop][0].'</td>
<td>'.$field[$loop][1].'</td>
<td>'.$field[$loop][2].'</td>
<td>'.$field[$loop][3].'</td>
<td>'.$field[$loop][4].'</td>
<td>'.$field[$loop][5].'</td>
<td>'.$field[$loop][6].'</td>
<td>'.$field[$loop][7].'</td>
<td>'.$field[$loop][8].'</td>
<td>'.$field[$loop][9].'</td>
<td>'.$field[$loop][10].'</td>
<td>'.$field[$loop][11].'</td>
<td>'.$field[$loop][12].'</td>
<td>'.$field[$loop][13].'</td>
<td>'.$field[$loop][14].'</td>
<td>'.$field[$loop][15].'</td>
<td>'.$field[$loop][16].'</td>
</tr>';
$fp++;
}
fclose($fp);
?>
You can see what it is doing here http://www.smartphonesoft.com/fred/xmlfeed/test/itunes_to_mysql.php
You can paste your textfile into one $var, by jaust adding all with .= , as long as your file is not 2GB large.
Note: string can be as large as 2GB.