BorysSokolov Posted December 15, 2012 Share Posted December 15, 2012 Hello. I have a quick question (it's probably easy to solve): why isn't the below code working? <?php $myFile = "testFile.txt"; $fh = fopen($myFile, 'w+'); $theData = fread($fh, 5); fclose($fh); echo $theData; ?> All I get is a blank page. Nothing is displayed, and I'm not sure what to do. Quote Link to comment https://forums.phpfreaks.com/topic/272021-file-contents-wont-echo/ Share on other sites More sharing options...
kicken Posted December 15, 2012 Share Posted December 15, 2012 Your file does not contain any data to be read. Even if you put data into it before running the script, it won't have any in it anymore by the time it reaches the fread call due to: '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. The the manual page for fopen for details on what mode to use and when. Quote Link to comment https://forums.phpfreaks.com/topic/272021-file-contents-wont-echo/#findComment-1399502 Share on other sites More sharing options...
Christian F. Posted December 15, 2012 Share Posted December 15, 2012 I'd use file_get_contents () instead, as you don't have to mes about with file pointers and such then. Just replace the last four lines of your code with this: echo file_get_contents ($myFile); Quote Link to comment https://forums.phpfreaks.com/topic/272021-file-contents-wont-echo/#findComment-1399510 Share on other sites More sharing options...
BorysSokolov Posted December 15, 2012 Author Share Posted December 15, 2012 (edited) Your file does not contain any data to be read. Even if you put data into it before running the script, it won't have any in it anymore by the time it reaches the fread call due to: The the manual page for fopen for details on what mode to use and when. When I woke up today morning, I looked over the code and realized I forgot to include any text in the file; I must have been really tired yesterday. Anyway, I changed the fread attribute, like you recommended, but the code still isn't being displayed: <?php $myFile = "testFile.txt"; $fh = fopen($myFile, 'a+'); fwrite($fh, "Hello World!"); $theData = fread($fh, 5); fclose($fh); echo $theData; ?> Edited December 15, 2012 by BorysSokolov Quote Link to comment https://forums.phpfreaks.com/topic/272021-file-contents-wont-echo/#findComment-1399571 Share on other sites More sharing options...
DavidAM Posted December 16, 2012 Share Posted December 16, 2012 Try again fopen 'a+' Open for reading and writing; place the file pointer at the end of the file. If the file does not exist, attempt to create it. Hint: If you want to read data from a file, you need to start somewhere before the end of all of the data in the file. And, since you are not writing to the file in that code, you don't need to open it "for writing", just "for reading" Quote Link to comment https://forums.phpfreaks.com/topic/272021-file-contents-wont-echo/#findComment-1399722 Share on other sites More sharing options...
BorysSokolov Posted December 17, 2012 Author Share Posted December 17, 2012 Try again fopen Hint: If you want to read data from a file, you need to start somewhere before the end of all of the data in the file. And, since you are not writing to the file in that code, you don't need to open it "for writing", just "for reading" Right, that's a stupid mistake on my part. Works fine now, though. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/272021-file-contents-wont-echo/#findComment-1399771 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.