PHP 导出到文本文件 - 仅保存第一行

发布于 2024-10-11 08:00:59 字数 595 浏览 5 评论 0原文

我正在尝试将一些提取的 $_POST 信息导出到文本文件中,但是我的代码仅捕获第一个变量并忽略其余变量。我以这种方式将信息保存到文本文件中:

$values = "First Name: $fName\r\n";

$values .= "Last Name: $lName\r\n";

$values .= "Address: $address\r\n";

等等。

这是我用来写入文本文件的代码:

$fp = @fopen("person.data", "w") or die("Couldn't open person.data for writing!");
$numBytes = @fwrite($fp, $values) or die("Couldn't write values to file!");

@fclose($fp);

关于为什么它只保存第一个 $values ($fName) 变量而不保存其余变量的任何想法他们当中?它实际上保存了所有变量的 $values 字符串的第一部分(因此我在文本文件中的不同行上看到姓氏:、地址:等),但被调用的变量 $lName 和 $address 不会出现。

I'm trying to export some extracted $_POST information into a text file, however my code is only capturing the first variable and ignoring the rest. I'm saving the information to the text file in this manner:

$values = "First Name: $fName\r\n";

$values .= "Last Name: $lName\r\n";

$values .= "Address: $address\r\n";

etc.

This is the code I use to write to the text file:

$fp = @fopen("person.data", "w") or die("Couldn't open person.data for writing!");
$numBytes = @fwrite($fp, $values) or die("Couldn't write values to file!");

@fclose($fp);

Any ideas on why it would only save the first $values ($fName) variable but not the rest of them? It actually saves the first part of the $values string for all of them (so I see Last Name:, Address:, etc. on separate lines in the text file) but the called variables $lName and $address do not appear.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

半衬遮猫 2024-10-18 08:01:00

只需使用

file_put_contents('person.data', $value);

参见 http://de.php.net/manual/ en/function.file-put-contents.php

您可以使用常量 PHP_EOL 代替 "\r\n",它将包含正确的换行符您正在运行脚本的平台的字符。

Just use

file_put_contents('person.data', $value);

See http://de.php.net/manual/en/function.file-put-contents.php

Instead of "\r\n" you can use the constant PHP_EOL, which will contain the correct newline character(s) for the platform you are running the script on.

顾挽 2024-10-18 08:01:00

我看到变量是 $lName - 这是区分大小写的事情吗?如果你回应同样的事情会发生什么?

I see the variables are $lName - is it a case sensitivity thing? What happens if you echo the same thing?

甜宝宝 2024-10-18 08:01:00

我解决了这个问题 - 问题是我没有在我创建的隐藏表单中命名其他部分,就像我为这个人的名字所做的那样。

I figured out the issue - the problem was I wasn't naming the other pieces within a hidden form I created, like I did for the person's first name.

尽揽少女心 2024-10-18 08:01:00
$values = "First Name: $fName\r\n";

$values .= "Last Name: $lName\r\n";

$values .= "Address: $address\r\n";
$values = "First Name: $fName\r\n";

$values .= "Last Name: $lName\r\n";

$values .= "Address: $address\r\n";
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文