phpnewbieca Posted October 24, 2009 Share Posted October 24, 2009 fopen(contact_us.txt"a+") is not creating the file contact_us.txt. The 'a+' should create a file if it doesn't exist and append the information in $str to the beginning of the file 'rewhind($fh)'. What am I doing wrong? function Order(){ global $Date, $confirmation_number, $send_to, $first_name, $last_name, $suffix, $email, $telephone, $comments; $myFile = "contact_us.txt"; $fh = fopen($myFile,'a+') or die("can't open <i>$myFile</i>"); if(!$fh) { die("couldn't open file <i>$myFile</i>"); } else { rewind($fh); $str.= " Date: $Date\r\n"; $str.= " Confirmation Number: $confirmation_number\r\n"; $str.= " To: $send_to\r\n"; $str.= " First Name: $first_name\r\n"; $str.= " Last Name: $last_name\r\n"; $str.= " Suffix: $suffix\r\n"; $str.= " Email Address: $email\r\n"; $str.= " Telephone: $telephone\r\n"; $str.= " Comments: $comments\r\n"; fwrite($fh, $str); echo "success writing to file"; } fclose($fh); } [/Code] Quote Link to comment https://forums.phpfreaks.com/topic/178846-solved-fopentxta-not-creating-file-and-writing-to-it/ Share on other sites More sharing options...
DavidAM Posted October 24, 2009 Share Posted October 24, 2009 Is error reporting on? Are you getting any error messages? Put error_reporting(E_ALL); ini_set("display_errors", 1); at the beginning of your scripts. Then post any errors you get so we can help. Also, do you realize that rewind simply moves the pointer to the beginning of the file? The fwrite will OVERWRITE existing content in the file, it will NOT INSERT the new data into the beginning of the file. Quote Link to comment https://forums.phpfreaks.com/topic/178846-solved-fopentxta-not-creating-file-and-writing-to-it/#findComment-943572 Share on other sites More sharing options...
phpnewbieca Posted October 26, 2009 Author Share Posted October 26, 2009 I will place the error reporting at the beginning of my script. I am confused, I thought fopen(myFile,"a+") created/opened for appending the information to it. therefore the information written to the file by fwrite($fh,$str) would be appended to the file. If this is not correct, how do I append/add information to a existing file? Quote Link to comment https://forums.phpfreaks.com/topic/178846-solved-fopentxta-not-creating-file-and-writing-to-it/#findComment-944605 Share on other sites More sharing options...
Adam Posted October 26, 2009 Share Posted October 26, 2009 Well what exactly does happen when you run the code? You are right though that using a+ will attempt to create the file if it doesn't exist and then append to the end. Edit: Didn't see what you said at first. The way the file write functions work would mean that moving the pointer to the start of the file would over-write the text currently at the start of the file. Take a look at the manual for rewind and look at the examples. May need to re-think what you're trying to do here. Quote Link to comment https://forums.phpfreaks.com/topic/178846-solved-fopentxta-not-creating-file-and-writing-to-it/#findComment-944606 Share on other sites More sharing options...
phpnewbieca Posted October 27, 2009 Author Share Posted October 27, 2009 DavidAM I put the error codes you recommend at the top of my script: Line 3: error_reporting(E_ALL); Line 4: ini_set("display_errors", 1); This it the error message I received: Notice: Use of undefined constant 1 - assumed ' 1' in /home/xgasq39z/public_html/contact.php on line 4 Do you know why I am receiving this Notice? MrAdam The reason fopen() was not writing the file was I had some other errors I had to correct. After I corrected the errors fopen($myFile, "a+") worked. rewind($fh) worked for me. It rewind the file to the top. The appended information was added to the top of the file. I do have another question. The line of code echo " success writing to file"; produces the following in my browser (Firefox) "success writing to file1." Where does the '1' come from? Quote Link to comment https://forums.phpfreaks.com/topic/178846-solved-fopentxta-not-creating-file-and-writing-to-it/#findComment-945532 Share on other sites More sharing options...
DavidAM Posted October 27, 2009 Share Posted October 27, 2009 Oops, the ini_set() function is expecting the second argument to be a string, So I guess it should be ini_set("display_errors", "1");. I don't usually use this function because my configuration is setup to always display errors. I DO always put the other function in every script. Without these settings, you don't see errors or warnings, and you end up with code that can become problematic later. Quote Link to comment https://forums.phpfreaks.com/topic/178846-solved-fopentxta-not-creating-file-and-writing-to-it/#findComment-945748 Share on other sites More sharing options...
phpnewbieca Posted October 30, 2009 Author Share Posted October 30, 2009 Thank you! Quote Link to comment https://forums.phpfreaks.com/topic/178846-solved-fopentxta-not-creating-file-and-writing-to-it/#findComment-947948 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.