Page 1 of 1

php help

Posted: Fri Mar 12, 2004 10:17 pm
by Master of Puppets_it
I've just started using php, and have used it to make some little things, like a translator of sorts, an info page, and some other thing kind like a forum. Anyway, I'm making a guestbook, and i want each new entry to be placed at the beginning of the file without erasing the existing data, so adding it. Whenever I try to do it with fopen("file.hmt","r+"), it writes over the existing data, and i need it to add to it. Help please. thanks

Archived topic from Iceteks, old topic ID:2139, old post ID:18213

php help

Posted: Fri Mar 12, 2004 11:12 pm
by Red Squirrel
This is tricky. What you need to do is write it to a temp file, then read the current file and append it to the temp file, delete the current file and rename the temp file to the current file's name.

So the steps would go like this:

-write data to file2.txt
- append file.txt's content to file2.txt
- delete file.txt
- rename file2.txt to file.txt

That's how I used to do it. Now I use mysql for mostly everything so it works differently. You can sort by any collum you want when you fetch the data, it's actually easier then file management.

Archived topic from Iceteks, old topic ID:2139, old post ID:18215

php help

Posted: Sat Sep 25, 2004 9:46 pm
by wtd
A database is the way to do this, but it can also be done more efficiently with files. Don't create temp files, especially in a web server environment. What happens if two people want to do the same thing at the same time and both try writing to that same file at the same time? Well, the universe pretty much collapses in on itself and reality ceases to exist.

Do you want to collapse the universe?

Well, even if you do, don't!

My PHP's a bit rusty, but something like the following should work.

Code: Select all

<?php
   function update_data_file($new_text)
   {
      $data_file = fopen("data_file.txt", "r+"); // open the file
      flock($data_file, LOCK_EX); // put an exclusive lock on it
      $data_text = fread($data_file, filesize($data_file)); // get the text in the file already
      $output_text = $new_text . $data_text; // accumulate the new text
      fwrite($data_file, $output_text); // write out the data
      flock($data_file, LOCK_UN); // release file lock
      fclose($data_file); // close the file
   }
?>[code]  

[color=#888888][size=85]Archived topic from Iceteks,  old topic ID:2139, old post ID:21968[/size][/color]

php help

Posted: Sat Sep 25, 2004 10:27 pm
by Red Squirrel
Wow, so if we can control the universe with php, does that mean the universe all started with <?php ? :D

Archived topic from Iceteks, old topic ID:2139, old post ID:21969

php help

Posted: Sat Sep 25, 2004 10:31 pm
by wtd
No, but collapsing the universe by trying to write to the same file at the same time from two different instances of a program can be disasterous.

Archived topic from Iceteks, old topic ID:2139, old post ID:21972

php help

Posted: Sat Sep 25, 2004 10:33 pm
by Red Squirrel
Actually I seriously never thought of that... I used to use temp files all the time. I named them staticly too, to make things worse. I could of named them after the date, user's IP and MD5 string of user agent, then MD5 the whole thing. You know, just to make sure there won't be a file called the same, during that moment if by chance someone else needs that function. But now most of the site is mysql driven so less files. Files are good for simple things, but for more complex and traffic heavy stuff mysql or other db type is a must.

Archived topic from Iceteks, old topic ID:2139, old post ID:21974

php help

Posted: Sat Sep 25, 2004 11:00 pm
by wtd
You can still manage it with files if you're careful. There are SQL interfaces to plain text file databases.

Archived topic from Iceteks, old topic ID:2139, old post ID:21978