napalm Posted August 28, 2008 Share Posted August 28, 2008 Hey, I got me a very basic flatfile guestbook that im modifying for my website.. I need to have it where it created the txt file for the files page.. Like as you can see http://animatorhost.com/index.php?page=viewf&file=338i0.jpg&name=NapalM I have the page that desplays that uploaded file, and it has a basic comment system under.. My issue is, to desplay the comments i use a include.. But i cant find a way to just have it when a persion trys to create a comment it automaticly creats the text file that i have it set up too.. like i have the text file set up to the name of the file .txt like, 338i0.jpg.txt.. But the only way i can get it to work is by its self when a users posts a comment it will create it but give the user a error saying its not found (this happens for the first commenter) because the file has not been created yet.. Do you get what im trying to say? heres what i was trying first.. <?php $myFile = "posts/$file.txt"; if(!file_exists("posts/$file.txt")) { die("Sorry No Comments!"); $fh = fopen($myFile, 'w') or die("can't open file"); fclose($fh); } else { $file=include("posts/$file.txt"); } ?> Hope some one can help -__- Quote Link to comment https://forums.phpfreaks.com/topic/121765-txt-file-creation/ Share on other sites More sharing options...
joecooper Posted August 28, 2008 Share Posted August 28, 2008 could you not use mysql to store the comments? Quote Link to comment https://forums.phpfreaks.com/topic/121765-txt-file-creation/#findComment-628163 Share on other sites More sharing options...
napalm Posted August 28, 2008 Author Share Posted August 28, 2008 No because my hosting has very very very limited mysql.. and its almost filled... ya know... Plus id just rather use flatfile.. feels easier for me. Quote Link to comment https://forums.phpfreaks.com/topic/121765-txt-file-creation/#findComment-628164 Share on other sites More sharing options...
BlueSkyIS Posted August 28, 2008 Share Posted August 28, 2008 feels easier for me. you'll find that feeling going away rapidly. Quote Link to comment https://forums.phpfreaks.com/topic/121765-txt-file-creation/#findComment-628167 Share on other sites More sharing options...
napalm Posted August 28, 2008 Author Share Posted August 28, 2008 hehe. can u just help? I mean i use mysql (i suck at databases) for my user system.. But i just want to use basic txt files for the comments... Quote Link to comment https://forums.phpfreaks.com/topic/121765-txt-file-creation/#findComment-628169 Share on other sites More sharing options...
wildteen88 Posted August 28, 2008 Share Posted August 28, 2008 heres what i was trying first.. <?php $myFile = "posts/$file.txt"; if(!file_exists("posts/$file.txt")) { die("Sorry No Comments!"); $fh = fopen($myFile, 'w') or die("can't open file"); fclose($fh); } else { $file=include("posts/$file.txt"); } ?> include does not return a value, so you cant assign it to a variable. Instead your should use file_get_contents. Quote Link to comment https://forums.phpfreaks.com/topic/121765-txt-file-creation/#findComment-628174 Share on other sites More sharing options...
napalm Posted August 28, 2008 Author Share Posted August 28, 2008 I belived i tryed.. that... can some one just code me up something that views the file.. and if its not found to create it? please. Quote Link to comment https://forums.phpfreaks.com/topic/121765-txt-file-creation/#findComment-628202 Share on other sites More sharing options...
matthew798 Posted August 29, 2008 Share Posted August 29, 2008 $ourFileName = "YOURFILE.txt"; $fh = fopen($YOURFILE, 'a') or die("Can't open file"); $stringData = "Bobby Bopper\n"; fwrite($fh, $stringData); fclose($fh); maybe? Quote Link to comment https://forums.phpfreaks.com/topic/121765-txt-file-creation/#findComment-628390 Share on other sites More sharing options...
napalm Posted August 29, 2008 Author Share Posted August 29, 2008 You copyed and pasted that from another site, i know cause i used it before.. An what it was doing is it just kept creating the file over and over when you would try and post the comment. so it wouldent add what you wrote cause it would write over it with the new text file.. Quote Link to comment https://forums.phpfreaks.com/topic/121765-txt-file-creation/#findComment-628534 Share on other sites More sharing options...
wildteen88 Posted August 29, 2008 Share Posted August 29, 2008 fopen will attempt to create the file if it doesn't exist, it shouldn't overwrite the file. In order for this to happen you should use the appropriate mode parameter for fopen. As a side note, matthew798 code is valid it should append the string "Bobby Bopper\n" to YOURFILE.txt each time the page loads. If its not working correctly then may be its a file permission problem. Quote Link to comment https://forums.phpfreaks.com/topic/121765-txt-file-creation/#findComment-628903 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.