[Site Home] [Forum Home] [Articles] [File DB] [News Archives]

Programming -> php help


(View original topic)


wtd - Sep-25-2004 server time
You can still manage it with files if you're careful. There are SQL interfaces to plain text file databases.

Red Squirrel - Sep-25-2004 server time
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.

wtd - Sep-25-2004 server time
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.

Red Squirrel - Sep-25-2004 server time
Wow, so if we can control the universe with php, does that mean the universe all started with <?php ? em320.gif

wtd - Sep-25-2004 server time
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
<?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
  }
?>

Red Squirrel - Mar-12-2004 server time
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.

Master of Puppets - Mar-12-2004 server time
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

(Showing 50 last posts, newest on top)