gilgimech Posted March 30, 2010 Share Posted March 30, 2010 What I'm tying to do is update a txt file using php. the form: <?php $file=fopen("news.txt","r") or die("Cannot open"); fclose($file); ?> <form action="../includes/news_update.php" method="post"> <textarea rows="25" cols="40" name="content"> <?php echo $file ?> </textarea><br> <input type="submit" value="Submit"> </form> update_news.php: <?php $content = $_POST["content"]; $file=fopen("news.txt","w") or die("Cannot open"); fwrite($content); fclose($file); ?> what I want to happen is the text from the file to populate the form field. Edit the field then post it to the file. I can't seem to get any of it to work. Quote Link to comment https://forums.phpfreaks.com/topic/197034-update-txt-file-with-php/ Share on other sites More sharing options...
Alex Posted March 30, 2010 Share Posted March 30, 2010 Your problem is that fopen does not return the content of the file, so $file won't contain what you're expecting. Instead to you need to use fread. If you want to get the full content of the file you can do something like this: <?php $fp = fopen("news.txt","r") or die("Cannot open"); $file = fread($fp, filesize('news.txt')); fclose($fp); ?> The problem with your updating is that for fwrite you need to pass the resource returned by fopen ($file, in your case). <?php $content = $_POST["content"]; $file=fopen("news.txt","w") or die("Cannot open"); fwrite($file, $content); fclose($file); ?> Quote Link to comment https://forums.phpfreaks.com/topic/197034-update-txt-file-with-php/#findComment-1034354 Share on other sites More sharing options...
gilgimech Posted March 30, 2010 Author Share Posted March 30, 2010 I tried what you suggested now I get the error Warning: fread() [function.fread]: Length parameter must be greater than 0 in /home/netgame2/public_html/pages/site_news.php on line 3 Quote Link to comment https://forums.phpfreaks.com/topic/197034-update-txt-file-with-php/#findComment-1034372 Share on other sites More sharing options...
Alex Posted March 30, 2010 Share Posted March 30, 2010 Well then that means your news.txt file contains nothing. To fix this before reading from the make sure there's actually something to read, like so: <?php if(($filesize = filesize('news.txt')) > 0) { $fp = fopen("news.txt","r") or die("Cannot open"); $file = fread($fp, $filesize); fclose($fp); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/197034-update-txt-file-with-php/#findComment-1034376 Share on other sites More sharing options...
gilgimech Posted March 30, 2010 Author Share Posted March 30, 2010 My file size is 1.287k and there is something in it. I replaced the code and the error is gone but nothing there is nothing in the textfeild. The news section here uses the same txt file. http://www.netgamegurus.com/index2.php Quote Link to comment https://forums.phpfreaks.com/topic/197034-update-txt-file-with-php/#findComment-1034378 Share on other sites More sharing options...
Alex Posted March 30, 2010 Share Posted March 30, 2010 Do you have the path correct? Quote Link to comment https://forums.phpfreaks.com/topic/197034-update-txt-file-with-php/#findComment-1034385 Share on other sites More sharing options...
gilgimech Posted March 30, 2010 Author Share Posted March 30, 2010 Yep it's in the same dir as the form Quote Link to comment https://forums.phpfreaks.com/topic/197034-update-txt-file-with-php/#findComment-1034386 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.