Jump to content

Reading Files


garyblackpool
 Share

Recommended Posts

Hi i have to files one reads the other file however it is not, instead it is returning a blank page and i am not sure what the problem is could someone eplease help?Thanks Gary

<head><title>SaveSonnet</title></head><body><?print $sonnet76$sonnet76 = <<<HERESonnet # 76, William ShakespereWhy is my verse so barren of new pride,For as the sun is daily new and old,So is my love still tell what is told,HERE;$fp = fopen("sonnet76.txt", "w");fputs($fp, $sonnet76);fclose($fp);?></body>

<style type = "text/css">body{	font-family:'Brush Script MT', script;	font-size:20pt;	}	</style>	<body>	<?	$fp = fopen("sonnet76.txt", "r");		// first line is title	$line = fgets($fp);	print "<center><h1>$line<//h1></center>\n";		print "<center>\n";	//print out rest of sonnet	while (!feof($fp)){		$line = fgets($fp);		print "$line <br>\n";		} //end while 			print "</center>\n";		fclose($fp);?></body>

Link to comment
Share on other sites

It's probably easier to use file_put_contents:

<?$sonnet76 = <<<HERESonnet # 76, William ShakespereWhy is my verse so barren of new pride,For as the sun is daily new and old,So is my love still tell what is told,HERE;file_put_contents('sonnet76.txt', $sonnet76);?>

To read it, you can either use file_get_contents to get everything and then explode it into an array of lines, or use the file function to just read the contents into an array of lines.

<?$lines = file('sonnet76.txt');	// first line is titleprint "<center><h1>{$lines[0]}</h1></center>\n";	print "<center>\n";//print out rest of sonnetfor ($i = 1; $i < count($lines); $i++)  echo $lines[$i] . '<br>';		print "</center>\n";	fclose($fp);?>

If that isn't working, check the text file to see if the text is getting written. If it's not, check to make sure that PHP has permission to create and write to files in that directory.

Link to comment
Share on other sites

Hi I tried the other code which you gave it did the same thing. The page was all black and at the bottom there was a warning although i could not read it all because that is in white. But when i looked at the page source like in my code. The variable and the warning:b>Warning</b>: fclose(): supplied argument is not a valid stream resourceThanks

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...