Jump to content

Editing a text file?


EchoFool

Recommended Posts

Hey,

 

Currently i have a chat room which calls my database quite frequently... so im thinking i can lower the heavy load from my server by taking it off MYSQL methods and using PHP read/write/delete methods of a text file.

 

Now i have two issues with this... first one is:

<?php
$myFile = "chatlist.txt";
$fh = fopen($myFile, 'w') or die("can't open file");
$stringData = $Message.'<br/>';
fwrite($fh, $stringData);
fclose($fh);
?>

 

This writes the most recent line, how ever the next line that gets sent overwrites so theres only ever one line in the txt file =/

 

Secondly how on earth do you delete a specific line from the file efficiently ? Say for example a line which broke the rules some how like spam was posted etc.

 

Hope you get shed light on this.

 

Thanks

Link to comment
Share on other sites

// Change the fopen mode from 'w' to 'a+'
$fh = fopen($myFile, 'a+') or die("can't open file");

and for future reference, see the manual.

 

Secondly how on earth do you delete a specific line from the file efficiently ? Say for example a line which broke the rules some how like spam was posted etc.

You don't. Thats why God invented databases. In all honesty MySQL will always be faster and safer than a flat file, and can handle tons of requests in a short amount of time. Try changing the way you access the database if it really is slowing you down.

Link to comment
Share on other sites

Well, unfortunately its impossible to provide a solution without seeing all of the code and database, but I can say that using a flat file will definitely be slower and prone to corruption. Lets try to get some more info and hammer this out...

  • Are you hosting your own server or are you going through a hosting company?
  • Are your HTTP and MySQL servers running on the same machine? If not, what is their latency (ping)?
  • Does your php code that accesses the database get executed more frequently then needed?
  • Is it really necessary to post every new message as it arrives? Why not buffer some sql in a local text file that only gets executed after a set time? (This isn't the same as using text files as a database, appending is very fast)

 

Those are just a few things I could think of randomly, and I'm sure theres plenty more effecting your script's execution time. My suggestion is to really study your code and see where the most work is being done. Litter it with time() calculations to see how long your SQL queries take, and stuff like that. Plenty of chat rooms out there use php and MySQL and can handle huge loads; know this, and know that your code just needs optimization.

 

One last thing is the structure of your database, which can greatly effect the speed of your queries. I'm terrible at database design so don't ask me, but if you find that it may be the problem, hop on over to the MySQL forums and ask around.

Link to comment
Share on other sites

In order of bullet points:

 

1) Yes linux server

2) Yes same machine i only own one server

3) Umm currently i have "Post message" + "load chat logs" the latter only happens when ajax receives TRUE (aka a change has occured)

4)If i do not post message as it arrives it would not appear like a live chat (think like IRC)

Link to comment
Share on other sites

I'm leaving work right now so just a quick response: it seems like you're doing things right from a distance but, again, the answer is in your code. I just recently finished coding up something similar in function to a chat room using asynchronous techniques (I refuse to use the marketing term, ajax) and my script is making hundreds of queries every iteration without any noticeable latency. I used one table for raw data and one table for metadata, and initiated my script when the metadata was in the right state. Anyway, hopefully someone else can provide more insight than I or you find the reason soon!

 

Now its time to leave work and become inebriated. TGIF.  :o

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.