通过 php 写入文本文件
我在试图消磨时间时遇到了一个小问题。我将填写的表格内容写入文本文件,以便于阅读。目前,所有内容都放在一行中,因此我无法判断消息从哪里开始或结束。我写了一个这样的php:
$from = $_POST[from];
$friend = $_POST[friend];
$carrier = $_POST[carrier];
$message = stripslashes($_POST[message]);
if ((empty($from)) || (empty($friend)) || (empty($message))) {
header ("Location: sms_error.php");
}
else if ($carrier == "orange_mobile_network") {
$formatted_number = $friend."@orange_mobile_network.co.uk";
mail("$formatted_number", "SMS", "$message");
header ("Location: sms_success.php");
}
然后将发送短信/文本消息。之后我想将消息写入/存储/附加到 txt 文件中。所以我写道:
$myFile = "sms_Numbers_Mgs.txt";
$fh = fopen($myFile, 'a+') or die("can't open file");
$stringData = $_POST[from];
fwrite($fh, $stringData);
$stringData = $_POST[friend];
fwrite($fh, $stringData);
$stringData = $_POST[message];
fwrite($fh, $stringData);
fclose($fh);
但就像我说的。一切正常。我只是希望能够读取该文件并将所有内容放在新行中,并可能将其格式化以方便阅读。我不想使用数据库来存储 TXT,因为我的主机要向我收费。
I have a little issue while trying to kill time. I am writint to a text file the contents of the filled in form so it is easy to read. At the moment everything gets put int a single line, and therfore I can not tell where msgs begin or end. I wrote a php like this:
$from = $_POST[from];
$friend = $_POST[friend];
$carrier = $_POST[carrier];
$message = stripslashes($_POST[message]);
if ((empty($from)) || (empty($friend)) || (empty($message))) {
header ("Location: sms_error.php");
}
else if ($carrier == "orange_mobile_network") {
$formatted_number = $friend."@orange_mobile_network.co.uk";
mail("$formatted_number", "SMS", "$message");
header ("Location: sms_success.php");
}
Then the SMS/Text message will be sent. After this I wanted to write/store/append the message on a txt file. So I wrote:
$myFile = "sms_Numbers_Mgs.txt";
$fh = fopen($myFile, 'a+') or die("can't open file");
$stringData = $_POST[from];
fwrite($fh, $stringData);
$stringData = $_POST[friend];
fwrite($fh, $stringData);
$stringData = $_POST[message];
fwrite($fh, $stringData);
fclose($fh);
But like I said. Everything works. I just want to be able to read the file and put everything in a new line and possibly format it nicely for easy reading. I do not want to use a DB for storing the TXT as my host is going to charge me.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
更改此:
与此:
或:
change this:
with this:
or:
当您编写平台感知换行符时,您可以附加
PHP_EOL
。即$stringData =; 。 PHP_EOL;
如果不需要平台感知,使用 Windows 换行符是一个安全的选择。
You can append
PHP_EOL
when you write for a platform aware newline. I.e.$stringData = <string> . PHP_EOL;
If don't need it to be platform aware, going with the windows newline is a safe option.
我不太清楚因为你正在使用。 txt 而不是银行,但我对这种特定情况的建议是使用 json 格式。
示例:
很好,至少组织得更好。
一个拥抱!
I do not know exactly Cause you're using. txt instead of a bank, but my tip for this specific case is to work with json format.
Example:
good it is at least better organized.
a hug!