Burden to open and append text file with fopen

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
john1in2
Forum Newbie
Posts: 11
Joined: Tue Jan 13, 2009 5:50 pm

Burden to open and append text file with fopen

Post by john1in2 »

Hello!

Thanks to your help, I wrote a small php script using fopen (...,'a'); fwrite and fclose to write the IP address of visitors to my web site into a text log file.

viewtopic.php?f=1&t=93484&p=511298#p511298

Its been working great but now my file is approaching 200K.

My question for your theorists is....is there a cost to having php open and append text files when they get large? Will it slow down my site or can I just continue on as the thing gets up to a few MB?


- thanks
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: Burden to open and append text file with fopen

Post by josh »

Creating a handle is relatively inexpensive. Seeking a couple times is inexpensive, but lots of seeking would be expensive. Reading small amounts of data would be inexpensive reading large amounts would be expensive. Same with writing small amounts of data. appending involves creating a handle, seeking 1x to the end of the file, and then writing some data and closing a handle.
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Re: Burden to open and append text file with fopen

Post by Jenk »

If all you are doing is appending, I'd be tempted to just do:

Code: Select all

shell_exec("echo " . escapeshellarg($ipAddress) . " >> /path/to/file.txt");
Post Reply