Trying to limit log file.

I’m creating a custom log file and am looking for help on applying two options. I’m not applying both options together I’m just trying to get an understanding of how to apply the different options where I would be able to apply it in different situations. The php is as follows :

[php]
// Log IP Address Of All Visitors
$filename = $_SERVER[‘DOCUMENT_ROOT’] ."/Logs/IP_log(1).txt";
$handle = fopen($filename, “a”);
$content = "[ " . date( ‘M d, Y, g:i a’ ) . " ] - " . $_SERVER[‘REMOTE_ADDR’];
fwrite($handle, $content . “\n”);
fclose($handle);
$max_size = 500000;
if (filesize($filename) >= $max_size)) {
// Apply either option 1 or option 2
}
[/php]

The first option I’ve been attempting to use and have been failing at is the option to erase the content within, bringing the file size back to zero. The other option I’ve yet to attempt is once file size reaches max size is to create a new file with same name just apply the number change as follows :

IP_log(1).txt // The file that already exist
IP_log(2).txt
IP_log(3).txt
and so on.

On any attempts with the if statement I’ve been getting errors. I always try many different things before I post here on this site so I have made many attempts. The error is traced back to the if statement. Thanks to all who help give any assistance on either option. The option I would really like to learn most is to create a new file when file size is reached. Once again many thanks in advance for all who help with my learning php, I’m always appreciative.

Can I ask why you are not using a database for this?

As far as your questions go…

“I’ve been attempting to use and have been failing at is the option to erase the content within, bringing the file size back to zero”

I don’t see where you are attempting to do this? If you look on the fopen manual page you can see the different types of options. You would want to use one that specifies a truncate option. Like ‘w’

The next question “The other option I’ve yet to attempt is once file size reaches max size is to create a new file with same name just apply the number change”

I believe you would want to check the filesize before doing an fwrite. Then perhaps you could do a preg_replace to increment the file name. Maybe something like this:

[php]
$filename = “/path/to/Logs/IP_log(1).txt”;
if (filesize($filename) >= $max_size)) {
$filename = preg_replace("/((\d+))/e", ‘("$1" + 1)’, $filename);
}
[/php]

If you are not familiar with regex, basically what you see is the () escaped with ( and ) then the variable to match is (\d+) which is seen as $1. The /e means to treat the matches as variables which allows you to increment with $1 + 1

Also, as far as the file increment, since $filename will not always be (1) you will need additional code to determine what the latest filename is. Perhaps you could use PHP’s DirectoryIterator class to check the modified time of all files, or try to find the largest integer.

I still think this task is much easier accomplished using a database. You will not have to worry about writing to file or increment filenames. Simply store a timestamp + ip address in a database.

@m@tt - I believe that I’m going to do the database setup as a later on thing as this is all for testing right now. I’m just learning at this point. I’ve went ahead and attempted a few other things and came up with this which is working as wanted. The php is below.

[php]
// Log IP Address Of All Visitors
$filename = $_SERVER[‘DOCUMENT_ROOT’] ."/Logs/IP_log(1).txt";
$content = "[ " . date( ‘M d, Y, g:i a’ ) . " ] - " . $_SERVER[‘REMOTE_ADDR’];
$handle = fopen($filename, “a”);

if (filesize($filename) >= 1000000)) {
$filename = $_SERVER[‘DOCUMENT_ROOT’] ."/Logs/IP_log(";
$fileCount = count (glob ($filename . ‘*.txt’));
$newName = $filename . ( $fileCount + 1 . “)”) . ‘.txt’;
$handle = fopen($newName, ‘w’);
fwrite($handle, $content);
fclose($handle);
}
else {
fwrite($handle, $content . “\n”);
fclose($handle);
}
[/php]

Sorry for the late comment but I was attempting a lot of different things on this script. I know it’s better to put into a database but this is quite clever in it’s kind of way. I see it being able to be used for many different situations. I hope this is able helps to anybody with similar issues. Also @m@tt, I do appreciate your effort assistance.

Actually this isn’t working as I expected as I ran more test on it because the if statement is always going to be ran on the original file name and size, meaning once file size is reached the else statement becomes irrelevant and a new file is created every time. Sorry on jumping the gun there everyone. More suggestions on this topic will still be very helpful.

As m@tt has stated the file name will not always be the same so I’ve been attempting other stuff to determine the file size of last file entry and am not being successful. Below is the latest attempt.

[php]
$dir = dirname($_SERVER[‘DOCUMENT_ROOT’] ."/Logs/*.txt");
$lastMod = 0;
$lastModFile = ‘’;
foreach (scandir($dir) as $entry) {
if (is_file($dir.$entry) && filectime($dir.$entry) > $lastMod) {
$lastMod = filectime($dir.$entry);
$lastModFile = $entry;
}
}

// Log IP Address Of All Visitors

$content = "[ " . date( ‘M d, Y, g:i a’ ) . " ] - " . $_SERVER[‘REMOTE_ADDR’];
$handle = fopen($filename, “a”);

if (filesize($entry) >= 1000000)) {
$filename = $_SERVER[‘DOCUMENT_ROOT’] ."/Logs/IP_log(";
$fileCount = count (glob ($filename . ‘*.txt’));
$newName = $filename . ( $fileCount + 1 . “)”) . ‘.txt’;
$handle = fopen($newName, ‘w’);
fwrite($handle, $content);
fclose($handle);
}
else {
$filename = $_SERVER[‘DOCUMENT_ROOT’] ."/Logs/IP_log(1).txt";
$handle = fopen($filename, “a”);
fwrite($handle, $content . “\n”);
fclose($handle);
}
[/php]

The warning that I’m receiving is -

Warning: filesize(): stat failed for IP_log(1).txt.

I’m getting closer, but I’m not sure why filesize() is not working even though it finds the file.
:o

You are getting that error because $entry is never being defined. I’m guessing scandir is returning false so there is no loop.

$dir should be the path to /Logs do not include /*.txt

@m@tt - When I do it like that it doesn’t find the file. The error I get is :

Warning: filesize(): stat failed for Logs in “path to …Logs”

It doesn’t scan the file.
The script is still flawed regardless. I realize that because every time last file doesn’t reach >= file size limit. it will always go back to the else statement. instead of create new file. This is becoming more trickier. Looks like I’m just going to go the database route. So I will have to create a table with with all the info I want plus a pagination script for the listing of the data.

Why do you want to change logs based on filesize? Most of the logging I have seen uses a timestamp or date, either daily, weekly, monthly, etc. This way there is no need to search for the latest file and no need to increment a number.

So for example, instead of using IP_Log(1).txt you could generate IP_Log_Date.txt like,

$filename = “IP_Log_” . date(‘Y-m-d’) . “.txt”;

or something similar. The date could be rounded to the nearest week, or remove the -d for monthly.

@m@tt - that actually is cool and much more clever. Thanks this post can now be closed as date for file name makes it better and it increments starting a new file on date change. Once again thanks m@tt.

Sponsor our Newsletter | Privacy Policy | Terms of Service