php - How to get notification when a new text is added to a file?

31

There is a text file that errors from a script will be saved into.

Each error on a new line, Then these errors will be shown on a new admin menu.

Here is the code to save the errors:

$error = 'Error Message';
file_put_contents('erros.txt', $error.PHP_EOL , FILE_APPEND | LOCK_EX);

Getting the errors from that text file:

file_get_contents('errors.txt');

My question is how to create a notification when a new error is thrown to that file?

Something like when a new WordPress update, Or stackoverflow notifications.

Should I for example add some code beneath thefile_put_contants() function that creates the notification?

It doesn't be so fancy, Maybe just a red element without numbers, Something that tells there is a new text added.

WordPress Notifications Screenshot

993

Answer

Solution:

The first thing that comes into my mind: use an alias for file_put_contents and whenever you add text to the file you insert a value in a DB. At this point it is "easy" to know how many unread notifications you have.

473

Answer

Solution:

you could use filemtime to see if the file got modified since last check

http://php.net/manual/en/function.filemtime.php

People are also looking for solutions to the problem: Apache PHP project root handler / front controller pattern

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.