El Estrago Posted May 29, 2011 Share Posted May 29, 2011 So I'm working on a php page that can edit a .txt-file. This is what I'v got so far: <?php if (isset($_POST['submit'])) { $stringData = stripslashes($_POST['sf']); file_put_contents("huisrekening.txt", $stringData); header('Location: quaestor.php?a=update'); } ?> Huisrekening: <form action="" method="post"> <textarea name="sf" cols="100" rows="20"> <?php $theData = file_get_contents("huisrekening.txt"); echo $theData; ?></textarea> <br /> <input type="submit" name="submit" value="Update" /> </form> <?php if ($_GET['a'] == 'update') { echo ''; $myFile = "huisrekening.txt"; $fh = fopen($myFile, 'r'); $theData = fgets($fh); fclose($fh); echo $theData; } ?> Problem: each time I hit the Update button an extra line appears in the .txt-file. :-( Does anybody know what I did wrong? Quote Link to comment https://forums.phpfreaks.com/topic/237800-edit-txt-files-from-1-php-page/ Share on other sites More sharing options...
xyph Posted May 29, 2011 Share Posted May 29, 2011 It's possibly the linebreak in your textarea HTML. You're dealing with client-side data, so you may expect different results from different browsers. Your best bet is to trim() $stringData before writing it. Quote Link to comment https://forums.phpfreaks.com/topic/237800-edit-txt-files-from-1-php-page/#findComment-1221977 Share on other sites More sharing options...
El Estrago Posted May 29, 2011 Author Share Posted May 29, 2011 I'm just starting out with php. What exactly do you mean by trimming? Edit: This is what my goal is: a page with data, only one person (with password) is able to edit is via his browser (so without using ftp). I did play a little bit with Drupal, Wordpress and Joomla, but those CMS are too big and I have a perfect html/css website template (my own ^^) at the moment, don't want to change that. Quote Link to comment https://forums.phpfreaks.com/topic/237800-edit-txt-files-from-1-php-page/#findComment-1221979 Share on other sites More sharing options...
spiderwell Posted May 29, 2011 Share Posted May 29, 2011 it removes 'space' either end of a string, just put trim() around $stringData as he said. space being a line break or a space. Quote Link to comment https://forums.phpfreaks.com/topic/237800-edit-txt-files-from-1-php-page/#findComment-1221982 Share on other sites More sharing options...
xyph Posted May 29, 2011 Share Posted May 29, 2011 $stringData = trim( stripslashes($_POST['sf']) ); Quote Link to comment https://forums.phpfreaks.com/topic/237800-edit-txt-files-from-1-php-page/#findComment-1221984 Share on other sites More sharing options...
El Estrago Posted May 29, 2011 Author Share Posted May 29, 2011 $stringData = trim( stripslashes($_POST['sf']) ); This did the trick, thank you so much! Quote Link to comment https://forums.phpfreaks.com/topic/237800-edit-txt-files-from-1-php-page/#findComment-1221990 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.