Increase data of a file TXT in PHP

Can anyone help me? I do not know almost anything about PHP and I need some help.
I have the following situation:
I need to create a form (HTML and PHP) where my customer places his name, last name and his testimony so that it can be placed in my home page but without the use of database.
After looking for something in the net I tried to create the following code in PHP but I am not sure if it is correct and it will work.
The idea is to record data into text file and go increasing new testimony in the same file text.
The form in HTML is ready and PHP code I did is as follows:

<Php
$file = “http://www.mysite.com/mysite/testimony.txt” / / => I do not know if the path coding is correct (my server is a Linux)
$break = chr (13).chr (10) / / line break (jumps to the next line)
$fd = fopen ($file, “r”) / / opens the file testimony.txt for reading of the content and placing put the pointer at the beginning of the file
$content = chop(fgets($fd, 4096)) / / retrieves all the content written in the file into the variable CONTENT
fclose ($fd); / / closes the file
$fd = fopen ($file, “a”) / / Opens file to write putting the file pointer at the end of the file
fwrite (“firstname”.$break) / / Writes the FIRSTNAME of the client and makes a brake line
fwrite (“lastname”.$break) / / Writes the LASTNAME of the cliente and makes a brake line
fwrite (“text”.$break) / / Writes the TEXT testimony of the client and makes a brake line
fwrite ("-----------------------------------"); / / Makes a separation for each testimony
fwrite (“content”.$break) / / Writes the content of the erlier testimonies in the variable CONTENT and makes a break line
fclose ($fd); / / closes the file
?>

My questions:

  1. I don’t know if the “path” ir right in the above code (fopen) to open my file testimony.txt that is on my Linux server. (I realized that it and didn’t work. I think that codification is not right or maybe the file testimony.txt can not be used by permission problems (although it has been changed to 666 in my domain)
  2. The encoding must increase the customers by last testimony always at the top of the file testimony.txt as follows in the example below:

Mary
Lee
I really enjoyed the site! I recommend to everyone!

John
Balmer
Wonderful! The best I’ve ever seen …

August
Firenza
Simply amazing!

Is the code correct for this?

  1. Which is the command in html that I must into my page to read the file testimony.txt?

Thanks a lot.
Paulo

Hi Paulo :smiley:

May i suggest for a cleaner text file you don’t put all the dashes in there
(------------------) and place all the info on a single line
seperated by a tabspace \t then start a new line for the next entry \n

below are two little snippets, one to write the file,
and the other to read the file and how to print out the results as required.

[php]

<?php // write data. $file = "http://www.mysite.com/mysite/testimony.txt"; $fp = fopen ($file, "a"); // append flock($fp, LOCK_EX); // lock file so no one else can edit at same time fwrite($fp, "$firstname\t"); // firstname (and tabspace) fwrite ($fp, "$lastname\t"); // lastname (and tabspace) fwrite($fp, "$text\t"); // text (and tabspace) flock($fp, LOCK_UN); // unlock file fclose($fp); // close the file ?>

[/php]

[php]

<?php // REMEMBER TO CHANGE THE PATH!! // read data $file = "testimony.txt"; // Attempt to open the file if($fp = fopen($file, "rb")) { // If opened, loop through the file until reach the end while (!feof($fp) ) { // Get each line individually $line_of_text = fgets($fp, 1024); // Trim any spaces off $text[] = trim(stripslashes(str_replace("\t", '
', $line_of_text))); } // Close the file fclose($fp); } // uncomment the line below to display the last entry first! //krsort($text); // show the data foreach($text as $key => $value) { echo '

' . $value . '

------------------- '; // outputs the info. } /* outputs: Mary Lee I really enjoyed the site! I recommend to everyone! ------------------- John Balmer Wonderful! The best I've ever seen ... ------------------- August Firenza Simply amazing! ------------------- */ ?>

[/php]

(remember to add the form)

hope this helps,
:wink:

Edit: corrected typo in the ‘write’ file…

I thank you very much !

I’m going to try what you told me tomorrow !

Paulo

;D

Sponsor our Newsletter | Privacy Policy | Terms of Service