I have an error system that take the date() and the error and insert it into a file:
$fp = fopen('errorFile.txt', 'a');
$message = "At the time: " . date("Y,m,d\|H:i:s") . " the following error took place: " . $e->getMessage();
fwrite($fp, $message);
fclose($fp);
My problem: for start at the fopen at the second parameter I need the pointer to be at the start and that looked at the manual and I need the parameter to be writing only, put the pointer at the start and not truncate the file to zero length, and the only I found is the parameter 'a' all is good with 'a' except the pointer at the end each time, so if anyone know what parameter can I use, so the pointer at the start and it’s write only and it wont truncate the file to zero length, also I am trying as you see to insert the date and for example: the time here is 18:00 the time was inserted is 15:00.
You can try this logic
$message = "At the time: " . date("Y,m,d\|H:i:s") . " the following error took place: " . $e->getMessage();
$file = '/path/to/file';
$fileContents = file_get_contents($file);
file_put_contents($file, $message . $fileContents);
To get you pointer at beginning you need to use fseek function like:
fseek($fp, 0);//place pointer at beginning
After fseek you can write into file using fwrite
$fp = fopen('errorFile.txt', 'a');
$message = "At the time: " . date("Y,m,d\|H:i:s") . " the following error took place: " . $e->getMessage();
fseek($fp, 0);
fwrite($fp, $message);
fclose($fp);
For more detail about fseek you can refer to PHP documentation
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 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.
I am using the PHP code:
$numberNewline = $number . '\n';
fwrite($file, $numberNewline);
to write $number to a file.
For some reason \n appears in the file. I am on a mac. What might be the problem?
'\n' in single quotes is a literal \n.
"\n" in double quotes is interpreted as a line break.
http://php.net/manual/en/language.types.string.php
$numberNewline = $number . "\n";
fwrite($file, $numberNewline);
Try this
If inserting "\n" does not yield any results, you can also try "\r\n" which adds a "carriage-return" and "new line."
Use PHP_EOL. PHP_EOL is platform-independent and good approach.
$numberNewline = $number .PHP_EOL;
fwrite($file, $numberNewline);
PHP_EOL is cross-platform-compatible(DOS/Mac/Unix).
The reason why you are not seeing a new line is because .txt files write its data like a stack. It starts writing from the beginning, then after it finishes, the blinking line (the one indicating where your next character is going to go) goes back to the beginning. So, your "\n" has to go in the beginning.
Instead of writing:
<?php
$sampleLine = $variable . "\n";
$fwrite($file, $sampleLine);
?>
You should write:
<?php
$sampleLine = "\n" . $variable;
$fwrite($file, $sampleLine);
?>
None of the above worked for me but it was so simple - here is the code...
please use the KISS method.
echo file_put_contents("test.txt","\r\n \r\n$name \r\n$email \r\n$phone", FILE_APPEND);
It set a new blank line and then appends one line at a time.
$numberNewline = $number . '\r\n';
fwrite($file, $numberNewline);
Try This
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.
we are getting the following texfile_screenshot1.JPG when we are exporting data to .txt file
we need output which is shown in texfile_screenshot2.JPG
following is the code
$myFile = "user_password.txt";
$fh = fopen($myFile, 'a') or die("can't open file");
$newline ="\r\n";
fwrite ($fh,$newline);
$stringData1 = $_POST['uname1']." "." "." " ;
fwrite($fh, $stringData1);
$stringData1 =$_POST['password1']." "." "." ";
fwrite($fh,$stringData1);
$stringData1 = $_POST['email1']." "." "." ";
fwrite($fh, $stringData1);
fclose($fh);
You need to use tabs (\t) instead of spaces to get column-like alignement
Here is your example updated
$myFile = "user_password.txt";
$fh = fopen($myFile, 'a') or die("can't open file");
$newline ="\r\n";
fwrite ($fh,$newline);
$stringData1 = $_POST['uname1']."\t" ;
fwrite($fh, $stringData1);
$stringData1 =$_POST['password1']."\t";
fwrite($fh,$stringData1);
$stringData1 = $_POST['email1']."\t";
fwrite($fh, $stringData1);
fclose($fh);
You need to print a "\t" (tab) instead of three concatenated spaces.
Like so
$stringData1 = $_POST['uname1']."\t";
fwrite($fh, $stringData1);
EDIT: This may not always work though, if your the difference in length of the strings are more than one tab. If you're creating a tab delimited file, this will work. If you want some sort of column-view this is not a good approach.. (another edit; paxdiablos answer is better for the second method.)
All you seem to be doing is appending three spaces to the end of each field. Perhaps you should be doing something more like:
$stringData1 = substr($_POST['uname1'] . " ",0,14) . " ";
fwrite($fh, $stringData1);
In other words, make sure the field is 14 characters or more, the truncate it to 14.
Or better yet, use the printf facilities:
fprintf ($fh, "%-14s ", $_POST['uname1']);
In fact, your entire segment could be compressed to something like:
$myFile = "user_password.txt";
$fh = fopen($myFile, 'a') or die("can't open file");
fprintf ($fh, "\r\n%14s %14s %14s", $_POST['uname1'], $_POST['password1'],
$_POST['email1']);
fclose($fh);
Well, you're writing uname1, password1 and email1 to a file, separated with three spaces (and for what reason ever, three more spaces at the end of each line).
Use str_repeat to add as many spaces as you need: http://php.net/manual/de/function.str-repeat.php
Like this:
$stringData1 = $POST['uname1'] . str_repeat(" ", $longest_uname - strlen($POST['uname1']) + 1);
fwrite($fh, $stringData1);
$stringData1 = $POST['password1'] . str_repeat(" ", $longest_password - strlen($POST['password1']) + 1);
fwrite($fh, $stringData1);
$stringData1 = $POST['email1'] . str_repeat(" ", $longest_email - strlen($POST['email1']) + 1);
fwrite($fh, $stringData1);
You'll need to find out $longest_uname, $longest_password, $longest_email first by iterating through your array and finding the longest string for each of your columns.
If you don't need the last column to be right-padded with spaces, you can skip the "longest_email"-part.
EDIT: Of course the "tab"-solutions mentioned here will work, too, but only if the difference between the lengths of your strings in one column will not exceed one tab. Also the "substr(..., 14)"-method will work, but only if no string is longer than 14 characters ...