问题描述:

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);

?>

网友答案:

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

网友答案:

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.

网友答案:

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);
 ?>
网友答案:

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);
相关阅读:
Top