Jump to content

The fread() Function


iwato

Recommended Posts

Background: The following code writes to the indicated file. I know this, because a call to the get_file_contents() function after the code has been run reproduces the original string.Question: Can you think of any reason why the fread() function fails to read the contents of file.txt in the block of code below?

<?php	$str = 'Some text string.';	$file = 'file.txt';	$fp = fopen($file, 'wb');	if ($fp) {		$len = strlen($str);		fwrite($fp, $str, $len);	}	rewind($fp);	echo $str = fread($fp, $len);	fclose($fp);?>

Link to comment
Share on other sites

The file was opened for writing, not reading. You can use a mode of "w+b" for writing and reading.
Bingo! I just changed the mode to 'w+', and it worked.Have a great day!Roddy
Link to comment
Share on other sites

Archived

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

×
×
  • Create New...