View Full Version : php saving into text file.

10-29-2002, 05:51 AM
hey everyone...
i'm very new to php, it's my new project. Here's what I want to do:

I think if I explain my project first it will help. I am making a resturaunt's site and I want to allow the owner to go into a page and fill in a form of what the menu will be for a specific day. Then the "menu" page will show users what has been input for different dates like today, tomorrow, this weekend.

I can't use MySQL because they don't want to pay extra to add database support to the hosting plan... so I need to do this using text files... plus I don't think that there will be huge amounts of data, and speed isn't an issue.

So is there a way make the form info save into a text file, and then be accessed by the public page to display the info? I really really hope I can do this without using a database.


10-29-2002, 10:14 AM
Your main problem is going to be ascertaining filenames, deducing current date, working out which file represents today/tomorrow etc. Not too tricky - just worth spending a while thinking about it before jumping into the coding.

form to file...

if($_POST['course_title'] !== "") {
$file_name = "menu_files/tomorrows_menu.txt";
$file_pointer = fopen($file_name, "a");
$lock = flock($file_pointer, LOCK_EX);
if ($lock){
$data = '<h1>'.$_POST['course_title'].'</h1><br />'.$_POST['course_description'];
fputs($file_pointer, $data);
flock($file_pointer, LOCK_UN);
else {
echo '<form action="'.$PHP_SELF.'" method="post">';
title: <input type="text" name="course_title" /><br />
desc: <textarea name="course_description"></textarea>

Should get you heading in the right direction.
Note: the folder 'menu_files' would need to be chmodded to allow write access for user and group. Fly typed so might need debugging.

10-29-2002, 02:27 PM
thank you, Íkii. This might have been too big of a project for my first one, but thanks to your help I may be able to get it working and learn a lot in the process.
I'll see what I can do :)