PHP To Read a Text file

Hi,

I am wanting to read a text file located on a NAS device \hg-nas01\Logs\Test\Test.txt

The file has times and dates of Windows Updates and i would like to get a few items from it this file is always being written to it be below is a example

----WINDOWS UPDATES----- @ 31/07/2023 11:03:58 AM
Beginning our update search. Please note, this may take a few minutes.
Windows update search has successfully completed. Beginning iteration of result set...
-----Windows Updates----- @ 31/07/2023 11:04:19 AM
Total Updates:- 2
Important:-     1
Optional :-    1
--------Total Time: 20.92 Sec----------------

Iteration of Windows Update Search Results has successfully completed.
-------------------------

i would like to get the Total Updates, Important and Optional but the will have several versions of this string.

Can anyone assist in giving some examples the way to tackle this the logfile is written to every hour.

lets try this

<?php
// Get the file content
$file_content = file_get_contents("\\\\hg-nas01\\Logs\\Test\\Test.txt");

// Use regular expressions to find matches
preg_match_all("/Total Updates:- (\d+)/", $file_content, $total_updates);
preg_match_all("/Important:- (\d+)/", $file_content, $important_updates);
preg_match_all("/Optional :- (\d+)/", $file_content, $optional_updates);

// Print the results
foreach ($total_updates[1] as $index => $total) {
    echo "Total Updates: " . $total . "\n";
    echo "Important Updates: " . $important_updates[1][$index] . "\n";
    echo "Optional Updates: " . $optional_updates[1][$index] . "\n\n";
}
?>

Please replace the \\\\hg-nas01\\Logs\\Test\\Test.txt path with your actual NAS path if different.

Also, ensure that the PHP script has the necessary read permissions to access the file on the NAS. Sometimes, you may need to mount the NAS as a network drive in your operating system and then use the local mount path.

Hope this works for you, good luck

Hi Katsry,

I am getting a error of the following on testing your code i will try and work this out well hopefully :smile:

PHP Warning:  Undefined array key 0 in C:\inetpub\wwwroot\SupportDesk\Test.php on line 13
PHP Warning:  Undefined array key 0 in C:\inetpub\wwwroot\SupportDesk\Test.php on line 14
PHP Warning:  Undefined array key 1 in C:\inetpub\wwwroot\SupportDesk\Test.php on line 13
PHP Warning:  Undefined array key 1 in C:\inetpub\wwwroot\SupportDesk\Test.php on line 14
PHP Warning:  Undefined array key 2 in C:\inetpub\wwwroot\SupportDesk\Test.php on line 13
PHP Warning:  Undefined array key 2 in C:\inetpub\wwwroot\SupportDesk\Test.php on line 14

The warning is indicating that the array keys you’re trying to access do not exist. This can happen when the regular expressions do not find any matches in the file content, so the resulting arrays $total_updates[1] , $important_updates[1] , and $optional_updates[1] are empty.

// Check if matches were found before printing the results
if(!empty($total_updates[1]) && !empty($important_updates[1]) && !empty($optional_updates[1])){
    foreach ($total_updates[1] as $index => $total) {
        echo "Total Updates: " . $total . "\n";
        echo "Important Updates: " . $important_updates[1][$index] . "\n";
        echo "Optional Updates: " . $optional_updates[1][$index] . "\n\n";
    }
} else {
    echo "No updates found.";
}

Keep in mind that this solution assumes that for every “Total Updates”, there is an “Important Updates” and an “Optional Updates” value, and they all have the same index. If that’s not the case, you would need a more robust solution to handle missing values.

Sponsor our Newsletter | Privacy Policy | Terms of Service