Re: Help with reading, writing, opening etc.

From: Date: Thu, 28 Dec 2000 18:16:04 +0000
Subject: Re: Help with reading, writing, opening etc.
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-32119@lists.php.net to get a copy of this message
First off, you probably should be using a database, and not munging a text file. This is particularly true if it's even *remotely* possible to have *two* administrators. As soon as you have two administrators, you have a major problem with your file -- They might *both* try to edit it at the exact same time, and your file will go kablooey. Trust me on this one. A database will take care of this "race" condition for you -- That's one of the major bonuses of using a database. Also, for the features you plan (edit, delete, etc) a database is indispensible -- Trying to do all that in a text file is going to be a nightmare. You will essentially be re-inventing a major chunk of a database's functionality, only doing it really, really badly. [Sorry, but the database guys have been doing this for *years* and a bazillion people have worked on it...] That said, if you only need to append to the file, you just need to do this: 1. Make the file writable by the PHP user*. DANGER: Unless you have root access, this usually means that you're stuck making the file world-writable. DANGER: World-writable makes it possible for any fellow user on your computer to edit it. DANGER: Even with root access, be careful to not make it executable -- chown it to the PHP user. 2. Open the file using "append" mode for the admin page: $file = fopen("/full/path/to/file", "a"); The PHP user is usually "nobody" or a custom non-empowered user named "www". <?php phpinfo();?> will tell you for sure what user PHP is running as. If, like many who went before you, you are having problems because you want to "append to the beginning of the file": You can't. That's not how a file works. You'd have to make a whole new file, put the new entry in it, tack on all the old stuff, and then copy that file over the old file. This is even *more* reason to use a database instead of a file. You can time-stamp the entries in a database, and present them in either order. Or any other order you can describe, for that matter. ----- Original Message ----- From: "ScreamKing" <goodfella_gf@hotmail.com> Newsgroups: php.general Sent: Thursday, December 28, 2000 7:30 AM Subject: [PHP] Help with reading, writing, opening etc. Hi All, PROBLEM Create three files - text.txt file - display.php file to display text.txt file (This opens text.txt for reading only) - admin.php file that updates the text.txt file (This should write to the text.txt file) I can get the display file to show the contents of the text.txt file, but am having severe difficulties with the admin.php and getting it to write to the text.txt file. (I can get it to write something like echo "Add this"; but I am not sure how to go about the following... Basically I am trying to make an admin page where someone can log on and use a form to submit some text. The text is then added (written) to the text.txt file. However despite trying allsorts, I am not sure how to do it. Any help would be appreciated. This is a bit like a news updater or something along those lines (As I want to make it easy for someone with no HTML/PHP experience to update a page by simply entering text and submitting). Any help with this would be appreciated! (Obviously as time passes, I would like to expand this into being able to edit, modify and delete existing data from the text.file, but for now, I am simply stuck on the main part). Thanks a lot, SK

« previous php.general (#32119) next »