Jump to content

Writing to a file


Striped Fish2

Recommended Posts

I've been playing with php and trying to write a simple AI program that will learn things you tell it and will store the stuff in text files. Anyway I am having an issue writing to the file I don't get a 500 it just doesn't write anything to the file. Also how do I make line breaks? Oh and how do I redirect to another page?Here's my code:

<?php//This array shows the possible greetings a user can type in.$greetings[0]="HI";$greetings[1]="HEY";$greetings[2]="HELLO";$usercommand=$_GET["command"];$command = strtoupper($usercommand);If ($command==$greetings[0] OR $command==$greetings[1] OR $command==$greetings[2])	 $cresponse="[AI] Who is it?";Else	 $cresponse="[AI] I don't understand tell me what does " . $usercommand . " mean?"; 	$newfiledata = "[You] " . $usercommand . "" . $cresponse . "";echo $newfiledata;echo file_put_contents("conversation.txt", $newfiledata, "FILE_APPEND");?>

Link to comment
Share on other sites

Oh well I'm actually planning on writing it as a computer application in Visual Basic because I want to eventaully add voice recognition to it. I'm just building a prototype in PHP to get all my thoughts together and come up with all the features.Anyway I have another problem I decided to work on it on a different computer because PHP was acting odd on my Desktop and taking 91% of the processor. I can write to the file but I can't read from it. Here's my code for the index.php page. Everytime I run it says the page has exceeded the 30 seconds and timed out.

<?phpecho "<html><head></head><body style='background:black;'>";echo "<div style='color:white;'>";$file = fopen("conversation.txt","r");while(!feof($file));{echo fgets($file);}echo "</div>";echo "<form action='proccesscommand.php'><input type='text' name='command' style='color:White;border:0px;border-bottom:1px solid white;";echo "background:black;width:100%;border-top: 1px solid white'/></form></body></html>";?>

What am I doing wrong?

Link to comment
Share on other sites

If you're just outputting the file, you could use readfile(). A loop is unnecessary.

<?phpecho '<html><head></head><body style="background:black;">';echo '<div style="color:white;">';readfile('conversation.txt');echo '</div>';echo '<form action="proccesscommand.php"><input type="text" name="command" style="color:White;border:0px;border-bottom:1px solid white;';echo 'background:black;width:100%;border-top: 1px solid white"/></form></body></html>';

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...