jacko310592 Posted November 30, 2009 Share Posted November 30, 2009 the follwoing code is the very bsic stage of a comment form/guest book, but im stuck at the moment as i cannot get the file im saving the data to to stop rewriting over the data which is already stored on it. <form method="POST" action=""> <input type="text" name="name" value=""><br/> <input type="text" name="email" value=""><br/> <textarea name="message" rows="5" cols="20"></textarea><br/> <input type="submit"> </form> <?php $name = $_POST['name']; $email = $_POST['email']; $message = $_POST['message']; $txtFile = "comments.txt"; chmod("comments.txt", 0777); $txtFile = fopen($txtFile, 'w'); $stringData = "{$name}<br/> {$email}<br/> {$message}"; fwrite($txtFile, $stringData); fclose($txtFile); ?> i found on a site that "chmod("comments.txt", 0777);" is supposed to let php edit the file without over-writing it, but it is just over-writing it, can anyone think why this is happening? thanks guys Link to comment https://forums.phpfreaks.com/topic/183478-simple-comment-formguest-book/ Share on other sites More sharing options...
blueman378 Posted November 30, 2009 Share Posted November 30, 2009 its your opening modifier, you are using $txtFile = fopen($txtFile, 'w'); notice the w? fopen() @php.net 'r' Open for reading only; place the file pointer at the beginning of the file. 'r+' Open for reading and writing; place the file pointer at the beginning of the file. 'w' Open for writing only; place the file pointer at the beginning of the file and truncate the file to zero length. If the file does not exist, attempt to create it. 'w+' Open for reading and writing; place the file pointer at the beginning of the file and truncate the file to zero length. If the file does not exist, attempt to create it. 'a' Open for writing only; place the file pointer at the end of the file. If the file does not exist, attempt to create it. 'a+' Open for reading and writing; place the file pointer at the end of the file. If the file does not exist, attempt to create it. 'x' Create and open for writing only; place the file pointer at the beginning of the file. If the file already exists, the fopen() call will fail by returning FALSE and generating an error of level E_WARNING. If the file does not exist, attempt to create it. This is equivalent to specifying O_EXCL|O_CREAT flags for the underlying open(2) system call. 'x+' Create and open for reading and writing; place the file pointer at the beginning of the file. If the file already exists, the fopen() call will fail by returning FALSE and generating an error of level E_WARNING. If the file does not exist, attempt to create it. This is equivalent to specifying O_EXCL|O_CREAT flags for the underlying open(2) system call. Link to comment https://forums.phpfreaks.com/topic/183478-simple-comment-formguest-book/#findComment-968505 Share on other sites More sharing options...
blueman378 Posted November 30, 2009 Share Posted November 30, 2009 hmm gotta remember to do the "Quote from:THE PHP.NET MANUAL" more often Link to comment https://forums.phpfreaks.com/topic/183478-simple-comment-formguest-book/#findComment-968511 Share on other sites More sharing options...
jacko310592 Posted November 30, 2009 Author Share Posted November 30, 2009 oh, i cant believe i missed that XD i guess 'a+' is the best to go with then thanks blueman378 Link to comment https://forums.phpfreaks.com/topic/183478-simple-comment-formguest-book/#findComment-968516 Share on other sites More sharing options...
blueman378 Posted November 30, 2009 Share Posted November 30, 2009 All good, and yes a+ is the best for you, dont forget to mark the topic as solved Link to comment https://forums.phpfreaks.com/topic/183478-simple-comment-formguest-book/#findComment-968519 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.