I have a file that I'm using to log IP addresses for a client. They want to keep the last 500 lines of the file. It is on a Linux system with PHP4 (oh no!).
我有一个文件,我用来记录客户端的IP地址。他们想要保留文件的最后500行。它在PHP4的Linux系统上(哦不!)。
I was going to add to the file one line at a time with new IP addresses. We don't have access to cron so I would probably need to make this function do the line-limit cleanup as well.
我打算用新IP地址一次添加一行文件。我们无权访问cron所以我可能需要让这个函数也进行行限制清理。
I was thinking either using like exec('tail [some params]')
or maybe reading the file in with PHP, exploding it on newlines into an array, getting the last 1000 elements, and writing it back. Seems kind of memory intensive though.
我想要使用像exec('tail [some params]')或者用PHP读取文件,在换行符中将其爆炸成数组,获取最后1000个元素,然后将其写回来。虽然看起来有点内存密集。
What's a better way to do this?
有什么更好的方法呢?
Update:
Per @meagar's comment below, if I wanted to use the zip functionality, how would I do that within my PHP script? (no access to cron)
Per @ meagar的评论如下,如果我想使用zip功能,我将如何在PHP脚本中执行此操作? (无法访问cron)
if(rand(0,10) == 10){ shell_exec("find . logfile.txt [where size > 1mb] -exec zip {} \;") }
Will zip enumerate the files automatically if there is an existing file or do I need to do that manually?
如果存在现有文件或者我是否需要手动执行此操作,zip会自动枚举文件吗?
3 个解决方案
#1
2
The fastest way is probably, as you suggested, to use tail:
正如你的建议,最快的方法可能是使用tail:
passthru("tail -n 500 $filename");
(passthru does the same as exec only it outputs the entire program output to stdout. You can capture the output using an output buffer)
(passthru与exec相同,只是将整个程序输出输出到stdout。您可以使用输出缓冲区捕获输出)
[edit]
I agree with a previous comment that a log rotate would be infinitely better... but you did state that you don't have access to cron so I'm assuming you can't do logrotate either.
我同意之前的评论,即日志轮换会无限好......但你确实声明你无法访问cron,所以我假设你也不能做logrotate。
#2
2
This would be the "proper" answer, and it's not difficult to set this up either.
这将是“正确的”答案,并且也不难设置它。
#3
0
You may get the number of lines using count(explode("\n", file_get_contents("log.txt"))) and if it is equal to 1000, get the substring starting from the first \n to the end, add the new IP address and write the whole file again. It's almost the same as writing the new IP by opening the file in a+ mode.
您可以使用count获得行数(explode(“\ n”,file_get_contents(“log.txt”))),如果它等于1000,则从第一个\ n开始到结尾的子字符串,添加新IP地址并再次写入整个文件。这与通过以+模式打开文件来编写新IP几乎相同。
#1
2
The fastest way is probably, as you suggested, to use tail:
正如你的建议,最快的方法可能是使用tail:
passthru("tail -n 500 $filename");
(passthru does the same as exec only it outputs the entire program output to stdout. You can capture the output using an output buffer)
(passthru与exec相同,只是将整个程序输出输出到stdout。您可以使用输出缓冲区捕获输出)
[edit]
I agree with a previous comment that a log rotate would be infinitely better... but you did state that you don't have access to cron so I'm assuming you can't do logrotate either.
我同意之前的评论,即日志轮换会无限好......但你确实声明你无法访问cron,所以我假设你也不能做logrotate。
#2
2
This would be the "proper" answer, and it's not difficult to set this up either.
这将是“正确的”答案,并且也不难设置它。
#3
0
You may get the number of lines using count(explode("\n", file_get_contents("log.txt"))) and if it is equal to 1000, get the substring starting from the first \n to the end, add the new IP address and write the whole file again. It's almost the same as writing the new IP by opening the file in a+ mode.
您可以使用count获得行数(explode(“\ n”,file_get_contents(“log.txt”))),如果它等于1000,则从第一个\ n开始到结尾的子字符串,添加新IP地址并再次写入整个文件。这与通过以+模式打开文件来编写新IP几乎相同。