php help

Computer builds, hardware and software discussion or troubleshooting, including peripherals. Essentially a general place to talk about desktop computers.
Locked
Master of Puppets_it
Posts: 15
Joined: Tue Feb 24, 2004 6:54 pm

php help

Post 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
User avatar
Red Squirrel
Posts: 29206
Joined: Wed Dec 18, 2002 12:14 am
Location: Northern Ontario
Contact:

php help

Post 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
Honk if you love Jesus, text if you want to meet Him!
wtd
Posts: 157
Joined: Sat Sep 25, 2004 7:21 pm

php help

Post 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]
User avatar
Red Squirrel
Posts: 29206
Joined: Wed Dec 18, 2002 12:14 am
Location: Northern Ontario
Contact:

php help

Post 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
Honk if you love Jesus, text if you want to meet Him!
wtd
Posts: 157
Joined: Sat Sep 25, 2004 7:21 pm

php help

Post 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
User avatar
Red Squirrel
Posts: 29206
Joined: Wed Dec 18, 2002 12:14 am
Location: Northern Ontario
Contact:

php help

Post 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
Honk if you love Jesus, text if you want to meet Him!
wtd
Posts: 157
Joined: Sat Sep 25, 2004 7:21 pm

php help

Post 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
Locked