如何将变量名添加到文件末尾?

How can I add variable names to the end of a file?

我创建了一个表单,用于将数据提交到服务器上的文件名。表单提交工作正常,它生成了名为 "we_input_.sts".

的请求文件

我正在尝试使用以下代码从 "bfstnme" 和 "gfstnme" 形式中获取两个变量并将它们附加到文件名,例如“wed_import-Jane_Bill.sts

这是修改后的代码:但是我仍然无法让它工作。

我正在尝试不同的想法来让它正常工作。我试过移动代码,但显然我仍然遗漏了一些东西。 $savestring== 之前的最后一行是 "$fp=fopen("wed-import-.sts", "a+");
$savestring 之后的最后一行是: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);
?>

为什么在写入文件之前不命名文件?

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

如果您想将信息放入文件中,您必须将 + 更改为 .像这样:

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

如果你想改变名字,你必须像@Jay_P说的那样做

让我们假设您在名为 $names 的变量中有名称。您可以像这样使用 FILE_APPEND 标志轻松地附加文本:

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

这是我在所有相关人员的宝贵帮助下解决它的方法。

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

上面的结果给我一个名为的文件名: “wed_Import_[JaneBill].sts。我只需要弄清楚如何在名字之间放置一个符号 (&)。 谢谢大家。