Anim9or Posted August 5, 2008 Share Posted August 5, 2008 Hi, I'm pretty new at PHP, as in I started yesterday, and have been following tutorials from ww3schools.com . Right now I'm up to reading and writing txt files and I keep getting the same error. This is the code: <?php $file=fopen("blah.txt","r"); echo $file; ?> Hower I keep getting: Resource id #6 When I open the page. I've searched around for people with simlar problems, but they always have trouble when they reach mySQL. Any help would be much obliged. Quote Link to comment https://forums.phpfreaks.com/topic/118302-early-on-help/ Share on other sites More sharing options...
.josh Posted August 5, 2008 Share Posted August 5, 2008 That's because fopen creates a resource stream for the file. You need to use it with for instance fread to read the contents. if you are wanting to just echo out the contents of a file, use file_get_contents Quote Link to comment https://forums.phpfreaks.com/topic/118302-early-on-help/#findComment-608796 Share on other sites More sharing options...
Anim9or Posted August 6, 2008 Author Share Posted August 6, 2008 Thanks for the help! However, I ran into a new problem on the way and I thought it might be easier to place here instead of start a new topic. I'm up to fwrite files now and no matter where I get my code from, I get the same errors, basically, I'm using this: $x=$_GET["suggestion"]; $filename = "data.txt"; $handle = fopen($filename, "r"); $contents = fread($handle, filesize($filename)); fwrite($contents, $x); echo $contents; fclose($handle); Now it will just write nothing now, however, if I change the "r" to a "w" or any other writing letters, I get this: Warning: fopen(data.txt) [function.fopen]: failed to open stream: Permission denied in /home/a4975390/public_html/default.php on line 11 PHP Error Message Warning: fread(): supplied argument is not a valid stream resource in /home/a4975390/public_html/default.php on line 12 PHP Error Message Warning: fclose(): supplied argument is not a valid stream resource in /home/a4975390/public_html/default.php on line 15 Thanks in advance! Quote Link to comment https://forums.phpfreaks.com/topic/118302-early-on-help/#findComment-609346 Share on other sites More sharing options...
dilum Posted August 6, 2008 Share Posted August 6, 2008 Give write permission to 'data.txt' file and try! Quote Link to comment https://forums.phpfreaks.com/topic/118302-early-on-help/#findComment-609351 Share on other sites More sharing options...
BioBob Posted August 6, 2008 Share Posted August 6, 2008 Just to expand on that, give Write Permissions to your web guest group so people that browse to your page have write permissions... But basically yeah, what he said... Quote Link to comment https://forums.phpfreaks.com/topic/118302-early-on-help/#findComment-609358 Share on other sites More sharing options...
Naez Posted August 6, 2008 Share Posted August 6, 2008 Try: <?php $x = $_GET["suggestion"]; $filename = "data.txt"; $handle = fopen($filename, "ab"); fwrite($handle, $x); $contents = fread($handle, filesize($filename)); echo $contents; fclose($handle); ?> I used "ab" in the handle because I assume you want to "append" the suggestion to the rest of the contents? Quote Link to comment https://forums.phpfreaks.com/topic/118302-early-on-help/#findComment-609369 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.