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? Link to comment https://forums.phpfreaks.com/topic/213183-another-extremely-simple-problem-this-time-its-fopen/ 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> Link to comment https://forums.phpfreaks.com/topic/213183-another-extremely-simple-problem-this-time-its-fopen/#findComment-1110069 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? Link to comment https://forums.phpfreaks.com/topic/213183-another-extremely-simple-problem-this-time-its-fopen/#findComment-1110079 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? Link to comment https://forums.phpfreaks.com/topic/213183-another-extremely-simple-problem-this-time-its-fopen/#findComment-1110080 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 Link to comment https://forums.phpfreaks.com/topic/213183-another-extremely-simple-problem-this-time-its-fopen/#findComment-1110081 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. Link to comment https://forums.phpfreaks.com/topic/213183-another-extremely-simple-problem-this-time-its-fopen/#findComment-1110082 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? Link to comment https://forums.phpfreaks.com/topic/213183-another-extremely-simple-problem-this-time-its-fopen/#findComment-1110084 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. Link to comment https://forums.phpfreaks.com/topic/213183-another-extremely-simple-problem-this-time-its-fopen/#findComment-1110085 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.