Trium918 Posted March 22, 2007 Share Posted March 22, 2007 This program is creating and writing to a file called project4.txt, but now I am trying to read to entire file so that it output would be some like a blog. Example: Click Here!!! <form method="POST"> <textarea name = "content" cols="100" rows="10"> </textarea><BR> <input type = submit name="submit"> </form> <? $content = $_POST['content']; @ $fp = fopen("project4.txt", "a"); flock($fp, 2); if (!$fp) { echo 'There is an error in this file!!'; exit; } fwrite($fp, $content); flock($fp, 3); fclose($fp); ?> Quote Link to comment https://forums.phpfreaks.com/topic/43755-solved-fopen-help/ Share on other sites More sharing options...
legohead6 Posted March 22, 2007 Share Posted March 22, 2007 use a database to make a blog, if your site gets bussy it will be like hell slow... Quote Link to comment https://forums.phpfreaks.com/topic/43755-solved-fopen-help/#findComment-212445 Share on other sites More sharing options...
Trium918 Posted March 22, 2007 Author Share Posted March 22, 2007 Its a school project, so I have to use fopen() method. Quote Link to comment https://forums.phpfreaks.com/topic/43755-solved-fopen-help/#findComment-212447 Share on other sites More sharing options...
Trium918 Posted March 22, 2007 Author Share Posted March 22, 2007 Can anyone help me with this program I have use fopen() for a school project Quote Link to comment https://forums.phpfreaks.com/topic/43755-solved-fopen-help/#findComment-212492 Share on other sites More sharing options...
kenrbnsn Posted March 22, 2007 Share Posted March 22, 2007 What is the problem you're having? Ken Quote Link to comment https://forums.phpfreaks.com/topic/43755-solved-fopen-help/#findComment-212503 Share on other sites More sharing options...
Trium918 Posted March 22, 2007 Author Share Posted March 22, 2007 click the Example link to get an idea of what I am talking about. Quote Link to comment https://forums.phpfreaks.com/topic/43755-solved-fopen-help/#findComment-212526 Share on other sites More sharing options...
kenrbnsn Posted March 22, 2007 Share Posted March 22, 2007 I clicked on the example. Is that what you're trying to do? Have you tried to write any code yet? Ken Quote Link to comment https://forums.phpfreaks.com/topic/43755-solved-fopen-help/#findComment-212529 Share on other sites More sharing options...
Trium918 Posted March 22, 2007 Author Share Posted March 22, 2007 I wrote the code above Quote Link to comment https://forums.phpfreaks.com/topic/43755-solved-fopen-help/#findComment-212531 Share on other sites More sharing options...
Trium918 Posted March 22, 2007 Author Share Posted March 22, 2007 My code accepts data from the user and enters it into a text file called project4.txt, but I am trying to get it to print output that is pulled from project4.txt. The user is then able to post as many post as he are she wants. Yes I am trying to do what is in the example Example: Click Here: <form method="POST"> <textarea name = "content" cols="100" rows="10"> </textarea><br /> <input type = submit name="submit"> </form> <? $content = $_POST['content']; $line = $_POST['line']; @ $fp = fopen("project4.txt", "a"); fwrite($fp, $content); fclose($fp); // readfile('project4.txt'); ?> Quote Link to comment https://forums.phpfreaks.com/topic/43755-solved-fopen-help/#findComment-212533 Share on other sites More sharing options...
kenrbnsn Posted March 22, 2007 Share Posted March 22, 2007 One way to do this would be to use the file() function. This function will read the entire file into an array, then you just have to loop through the array and output the information. Perhaps something like this: <?php $info = file('project4.txt'); for ($i=0;$i<count($info);$i++) { echo nl2br(htmlentities(stripslashes(trim($info[$i])),ENT_QUOTES)); echo '<hr>'; } ?> Ken Quote Link to comment https://forums.phpfreaks.com/topic/43755-solved-fopen-help/#findComment-212542 Share on other sites More sharing options...
Trium918 Posted March 22, 2007 Author Share Posted March 22, 2007 Ok, I change my code. I would like for someone to click on the links that I provided below. The first one is where I need to be and the second one is where I am now. You will noticed in the first Example the line brakes after the user click submit but Example2 doesnt. My code is Example2. Example1: Click Here Example2: Click Here <form method="POST"> <textarea name = "content" cols="100" rows="10"> </textarea><br /> <input type = submit name="submit"> </form> <? $content = $_POST['content']; $handle = $_POST['handle']; @ $fp = fopen("project4.txt", "a"); fwrite($fp, $content); $handle = @fopen("project4.txt", "r"); if ($handle) { while (!feof($handle)) { $buffer = fgets($handle, 100); echo $buffer."<br>"; } fclose($handle); } fclose($fp); // readfile('project4.txt'); ?> Quote Link to comment https://forums.phpfreaks.com/topic/43755-solved-fopen-help/#findComment-212544 Share on other sites More sharing options...
kenrbnsn Posted March 22, 2007 Share Posted March 22, 2007 Add a newline character to your fwrite() like this: <?php fwrite($fp, $content . "\r\n"); ?> Ken Quote Link to comment https://forums.phpfreaks.com/topic/43755-solved-fopen-help/#findComment-212548 Share on other sites More sharing options...
Trium918 Posted March 22, 2007 Author Share Posted March 22, 2007 Ken It works when I added the \r\n Quote Link to comment https://forums.phpfreaks.com/topic/43755-solved-fopen-help/#findComment-212552 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.