kalster Posted August 9, 2012 Share Posted August 9, 2012 i am writing to a file and then reading the contents of that file with "fopen w+", but i cant read the contents of that file. the output is blank. if i close the file and then reopen the file with the fopen "r" command then it works. why am i getting this strange behavior? <?php $do = "test.txt"; $file = fopen($do, 'w+') or die("file not found"); $go = "test"; fwrite($file, $go); //fclose($file); //$file1 = fopen($do, 'r') or die("file not found"); $re1 = fread($file, filesize($do)); fclose($file); echo $re1; ?> Quote Link to comment https://forums.phpfreaks.com/topic/266840-fread-no-output/ Share on other sites More sharing options...
maxudaskin Posted August 9, 2012 Share Posted August 9, 2012 http://www.php.net/manual/en/function.fopen.php If you read down to what the different modes are, you'll understand why. r Open for reading only; place the file pointer at the beginning of the file. w+ Open for reading and writing; place the file pointer at the beginning of the file and truncate the file to zero length. If the file does not exist, attempt to create it. Use r+ r+ Open for reading and writing; place the file pointer at the beginning of the file. Quote Link to comment https://forums.phpfreaks.com/topic/266840-fread-no-output/#findComment-1367998 Share on other sites More sharing options...
kalster Posted August 9, 2012 Author Share Posted August 9, 2012 yes, i have tryed r+, but the results are the same. i still do not get any output for fread after the fwrite call. Quote Link to comment https://forums.phpfreaks.com/topic/266840-fread-no-output/#findComment-1367999 Share on other sites More sharing options...
maxudaskin Posted August 9, 2012 Share Posted August 9, 2012 Disregard Try this and tell us what happens. <?php $file_name = 'test.txt'; $file = fopen($file_name, 'r+') or die ('File, "' . $file_name . '", was not found.'); $append = 'test'; fwrite($file, $append); $file_read = fread($file, filesize($file_name)); // Read a certain amount of bytes from the file (in this case, the entire file) $file_get_all = file($file_name); // Read the entire file into an array. Each line is has it's own key. print_r($file_read); echo '<br />---<br /><pre>'; print_r($file_get_all); echo '</pre>'; Quote Link to comment https://forums.phpfreaks.com/topic/266840-fread-no-output/#findComment-1368000 Share on other sites More sharing options...
maxudaskin Posted August 9, 2012 Share Posted August 9, 2012 Wait. I'm an idiot. There is good reason for why it does not read as you expected. When you write test to the file, it puts the pointer after the appended text. That means that it will output the EOF (end of file). Either reset the pointer, or use file(). Quote Link to comment https://forums.phpfreaks.com/topic/266840-fread-no-output/#findComment-1368001 Share on other sites More sharing options...
MMDE Posted August 9, 2012 Share Posted August 9, 2012 Wait. I'm an idiot. There is good reason for why it does not read as you expected. When you write test to the file, it puts the pointer after the appended text. That means that it will output the EOF (end of file). Either reset the pointer, or use file(). Or just use fclose() inbetween, which is always good practice to do after using fopen(). You can then re-open it and you should use the mode "r", to only read the file from the beginning of the file. Quote Link to comment https://forums.phpfreaks.com/topic/266840-fread-no-output/#findComment-1368010 Share on other sites More sharing options...
kalster Posted August 9, 2012 Author Share Posted August 9, 2012 ok, but what is the point of using "fopen r+" if it does not work in my case? that is, why use "fopen r+" at all? Quote Link to comment https://forums.phpfreaks.com/topic/266840-fread-no-output/#findComment-1368012 Share on other sites More sharing options...
Christian F. Posted August 9, 2012 Share Posted August 9, 2012 I would recommend not using fopen (), fwrite () or fread () at all. Just file_get_contents () and file_put_contents instead, and you'll find this whole problem will go away by itself. PS: Remember to use the correct flags, if you want to append instead of overwriting. Quote Link to comment https://forums.phpfreaks.com/topic/266840-fread-no-output/#findComment-1368077 Share on other sites More sharing options...
maxudaskin Posted August 9, 2012 Share Posted August 9, 2012 ok, but what is the point of using "fopen r+" if it does not work in my case? that is, why use "fopen r+" at all? When you write test to the file, it puts the pointer after the appended text. That means that it will output the EOF (end of file). Either reset the pointer, or use file(). This is the file: |Hello world from test.txt When you use fwrite, it moves the pointer from the beginning of the file, to the end. Hello world from test.txt| It then writes the string to the file, leaving the pointer at the end. Hello world from test.txtt| Hello world from test.txtte| Hello world from test.txttes| Hello world from test.txttest| To bring it back to the start, use rewind() Quote Link to comment https://forums.phpfreaks.com/topic/266840-fread-no-output/#findComment-1368114 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.