Jump to content

Writing to a file

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 post
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 post
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 post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...