3

I have created a form that submits the data to a filename on the server. The form submit is working fine, it generates the requested file called "we_input_.sts".

I am trying to use the following code to grab two variables from the form "bfstnme" and "gfstnme"and attach them to the filename eg "wed_import-Jane_Bill.sts

This is the amended code: However I am still unable to get it to work.

I am trying different ideas to get this to work correctly. I have tried moving the code around but I'm still obviously missing something. The last line before the $savestring== is "$fp=fopen("wed-import-.sts", "a+");
The last lines after the $savestring are : fwrite($fp,$savestring); fclose($fp);

<?php
$bfirstname = $_POST['bfstnme'];
$gfirstname = $_POST['gfstnme'];
$file = 'wed_import_.sts';
$current = file_get_contents($file);
$new_file = 'wed_input_'.$bfirstname.'&amp;'.$gfirstname.'.sts';
file_put_contents($new_file, $current);
?>
Adrian Cid Almaguer
  • 7,815
  • 13
  • 41
  • 63
  • I believe you will find the answer to this question [here](http://stackoverflow.com/questions/24972424/php-create-or-write-append-in-text-file) (( [php create or write/append in text file](http://stackoverflow.com/questions/24972424/php-create-or-write-append-in-text-file) )) – Baldvin Th Feb 15 '15 at 05:34
  • Please specify exactly what goes wrong with your code. In what way does it not work? File not output; exception; wrong data ... ? – Esoteric Screen Name Feb 15 '15 at 05:37
  • Thank you for commenting. The file with the correct data is being created without the inserted variables. I will try the suggestion below. –  Feb 15 '15 at 06:23
  • @Ivan Conway you got some advance? – Adrian Cid Almaguer Feb 19 '15 at 04:07
  • Hello Adrian, I have tried different variations of the following code and am able to create the required filename, however I am still unable to call the required $savestring values into the file. The php code works perfectly without the following code and generates the required "wed_import_.sts" file with all of the $savestring data in it.. I am obviously missing something or not putting the code in the right place. –  Feb 23 '15 at 09:32
  • I am currently trying variations of this without success. (.$bfstnme'_'.$gfstnme); $fp = fopen("wed_import_($names).sts", "a+"); –  Feb 23 '15 at 21:22

4 Answers4

1

Here is the way I have solved it using the valuable assistance of all concerned.

$names .= ("$bfstnme" . "$gfstnme");
$fp = fopen("wed_import_($names).sts", "a+");

The results of the above give me a filename called: "wed_Import_[JaneBill].sts. I only need to work out how to put an amperstand (&) betwen the names. Thank you to all.

0

Why you don't name the file before writing to it?

<?php
   $gfirstname = $_POST['gname'];
   $bfirstname = $_POST['bname'];
   $file = 'wed_input_Bride_Groom.sts';
   // Opens the file to get existing content hopefully
   $current = file_get_contents($file);
   // Appends bride and groom first names to the file hopefully
  $current .= ("gfirstname" . "bfirstname");
  $new_file = 'wed_input_'.$gfirstname.'_'.$bfirstname.'.sts';
  // Write the contents back to the file
 file_put_contents($new_file, $current);
 ?>
Adrian Cid Almaguer
  • 7,815
  • 13
  • 41
  • 63
Junius L
  • 15,881
  • 6
  • 52
  • 96
  • Thank you, that is more logical than the way I was going. I'll give it a try. –  Feb 15 '15 at 06:23
  • Thank you to all who have helped me. I now have the code working. Ivan –  Feb 23 '15 at 23:26
0

If you want put the info inside the file you must change the + by a . like this:

$current .= ("gfirstname" . "bfirstname");

If you want change the name, you must do something like @Jay_P says

Adrian Cid Almaguer
  • 7,815
  • 13
  • 41
  • 63
0

Let's assume you have the names in a variable called $names. You can easily append the text with the FILE_APPEND flag like this:

file_put_contents('wed_input_Bride_Groom.sts', $names, FILE_APPEND);