【问题标题】:How can I add variable names to the end of a file?如何在文件末尾添加变量名?
【发布时间】:2015-02-15 05:27:17
【问题描述】:

我创建了一个表单,将数据提交到服务器上的文件名。表单提交工作正常,它生成名为“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);
?>

【问题讨论】:

  • 相信你会找到这个问题的答案here((php create or write/append in text file))
  • 请准确说明您的代码出了什么问题。它以什么方式不起作用?文件不输出;例外;错误的数据...?
  • 感谢您的评论。在没有插入变量的情况下创建具有正确数据的文件。我会尝试下面的建议。
  • @Ivan Conway 你有预付款吗?
  • 您好 Adrian,我尝试了以下代码的不同变体并且能够创建所需的文件名,但是我仍然无法将所需的 $savestring 值调用到文件中。 php 代码无需以下代码即可完美运行,并生成所需的“wed_import_.sts”文件,其中包含所有 $savestring 数据。我显然遗漏了某些内容或没有将代码放在正确的位置。

标签: php file


【解决方案1】:

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

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

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

【讨论】:

  • 这样做$names .= ("$bfstnme" ."&amp;". "$gfstnme");
【解决方案2】:

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

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

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

【讨论】:

    【解决方案3】:

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

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

    【讨论】:

    • 谢谢你,这比我要走的路更合乎逻辑。我试试看。
    • 感谢所有帮助过我的人。我现在有代码工作。伊万
    【解决方案4】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-12-15
      • 2011-04-06
      • 1970-01-01
      • 1970-01-01
      • 2022-12-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多