Jump to content

writting to text file using php


Guest deadmouse
 Share

Recommended Posts

Guest deadmouse

Hello!I have an input page where users can input text (called the rumor mill), and it gets written to a text file to be called on to display a random string of text on a different page. The lines of text are seperated by --NEXT-- so the script knows where to seperate the lines. The problem I am having is that I wanted to limit the file to the 30 latest lines of inputed text (rumors). I just can't seem to get it right. When i use this code, i get this error "Parse error: parse error, unexpected T_STRING in /home/yearinbe/public_html/rumor/rumor.php on line 5"Here is the code:

<?php$fileName = 'rumor.txt';this is the part of code where i tried to limit it to 30 lines of inputted text.if ( isSet($_POST['submit']) && !empty($_POST['rumor']) ) {	if (!file_exists($fileName)) {  die ( 'File ' . $fileName . ' does not exist.' );	}		// Changed. Now opening the file and erasing it at the same time.	if (( $openFile = fopen($fileName, "w")  === false ) {  die ( 'Could not open file, Please try again' );	}		if ( flock( $openFile, LOCK_EX ) === true ) {	  // Added all this into the if statement  // Get the contents of the file  $rumorfile = file_get_contents($fileName);    // Explode it into an array  $explode = explode("--NEXT--", $rumorfile);     // Cut everything down until you have 29 or less quotes  do { 	 $cut_file = array(''); 	 for ($i = 0; $i < $rumors_in_file; $i++) {    // Replace the current part, denoted as $i,  with the next part    $next = $i + 1;    if ($next != $rumors_in_file) {   	 $cut_file[$i] = $explode[$next];    } 	 } 	 $explode = $cut_file; 	 $rumors_in_file = count($explode);  } while ($rumors_in_file >= 30);    // Add in your last quote into the last spot  $explode[$rumors_in_file] = $_POST['rumor'];  // Ok, time to convert the new array back into a string  // I think this is the correct syntax. If it doesn't work, check it on php.net  $rumorfile = "";  $i = 0;  foreach($explode as $quote) { 	 $rumorfile .= $quote; 	 if ($i < $rumors_in_file) {    $rumorfile .= "\n--NEXT--\n"; 	 } 	 $i++;  }  fwrite( $openFile, "$rumorfile " );  flock( $openFile, LOCK_UN );	} 		fclose( $openFile );}//The follow part is workingif ( isSet($_POST['submit']) && !empty($_POST['rumor']) ) {	if (!file_exists($fileName)) {  die ( 'File ' . $fileName . ' does not exist.' );	}	if (( $openFile = fopen( $fileName, 'a+' )) === false ) {  die ( 'Could not open file, Please try again' );	}	if ( flock( $openFile, LOCK_EX ) === true ) {  fwrite( $openFile, "\n--NEXT--\n{$_POST['rumor']}" );  flock( $openFile, LOCK_UN );	} 	fclose( $openFile );}?>

Here is a link to my files: rumor mill filesrumor1.php is the input page, rumor.txt is the text file that the info gets written to, and exfile.php is the page that calls upon the text file to display the text. If anyone could help, that would be great. I am really new to php, and i am not sure whats wrong.

Link to comment
Share on other sites

well I hardly know anything about php, but you've got an extra '(' on line fifteen of your code that you posted. remove that and see what happens. :) LG

if (( $openFile = fopen($fileName, "w")  === false ) {
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...