iwato Posted February 3, 2011 Share Posted February 3, 2011 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 More sharing options...
justsomeguy Posted February 3, 2011 Share Posted February 3, 2011 The file was opened for writing, not reading. You can use a mode of "w+b" for writing and reading. Link to comment Share on other sites More sharing options...
iwato Posted February 3, 2011 Author Share Posted February 3, 2011 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 More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.