Jump to content

[SOLVED] fopen(...txt,'"a+') not creating file and writing to it


Recommended Posts

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]

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.

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?

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.

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?

 

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.