Hangwire Posted September 11, 2010 Share Posted September 11, 2010 Hi all. What am I doing wrong here? I cannot read from the file with fopen, this is the code. <html> <body> <?php $file=fopen("welcome.txt","r"); ?> </body> </html> I get a blank page, although I have text written in that file. Whenever I set it to "w" it still shows nothing - when I check the file manually with notepad, I see that it's blanked - but I think thats meant to happen, after all "w" clears the file. Have I understood correctly? This still shows a blank page, but it says it can't find the file if it's not in the directory - how it should be. <html> <body> <?php $file=fopen("welcome2123.txt","r") or exit("Unable to open file!"); ?> </body> </html> tl;dr - Why doesn't fopen read anything from the txt file? Encodings match - everything is in UTF-8, although I doubt thats the problem... Help? Quote Link to comment Share on other sites More sharing options...
WatsonN Posted September 11, 2010 Share Posted September 11, 2010 here Ya go <html> <body> <?php $hfile = "welcome2123.txt"; $fh = fopen($hfile, 'r'); $theData = fread($fh, filesize($hfile)); fclose($fh); echo $theData; ?> </body> </html> Quote Link to comment Share on other sites More sharing options...
Hangwire Posted September 12, 2010 Author Share Posted September 12, 2010 Thank you so much! But if it's written like that in the W3 tutorial, shouldn't it work? Quote Link to comment Share on other sites More sharing options...
WatsonN Posted September 12, 2010 Share Posted September 12, 2010 It should but all I got was Resource ID #4 or something like that. Can ya post a link to it? Quote Link to comment Share on other sites More sharing options...
Hangwire Posted September 12, 2010 Author Share Posted September 12, 2010 Here you go. Yours works perfectly, I even understand it Quote Link to comment Share on other sites More sharing options...
WatsonN Posted September 12, 2010 Share Posted September 12, 2010 Thank ya I dont get theirs either, but mine is an append which means it will just keep adding on and on every time. Quote Link to comment Share on other sites More sharing options...
Hangwire Posted September 12, 2010 Author Share Posted September 12, 2010 Just a quick question, what does fclose do and why does it have to be there? Does it keep the file from going on forever and display the same thing over and over or... I got it wrong? Quote Link to comment Share on other sites More sharing options...
WatsonN Posted September 12, 2010 Share Posted September 12, 2010 I am by no means a expert but, fclose does what it says it closes the file so when you run other PHP or html code it wont try to do it inside fopen. that it my almost completely made up answer The real definition: The fclose() function causes the stream pointed to by stream to be flushed and the associated file to be closed. Quote Link to comment 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.