PHP 导出到文本文件 - 仅保存第一行
我正在尝试将一些提取的 $_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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
只需使用
参见 http://de.php.net/manual/ en/function.file-put-contents.php
您可以使用常量
PHP_EOL
代替"\r\n"
,它将包含正确的换行符您正在运行脚本的平台的字符。Just use
See http://de.php.net/manual/en/function.file-put-contents.php
Instead of
"\r\n"
you can use the constantPHP_EOL
, which will contain the correct newline character(s) for the platform you are running the script on.我看到变量是 $lName - 这是区分大小写的事情吗?如果你回应同样的事情会发生什么?
I see the variables are $lName - is it a case sensitivity thing? What happens if you echo the same thing?
我解决了这个问题 - 问题是我没有在我创建的隐藏表单中命名其他部分,就像我为这个人的名字所做的那样。
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.