通过 php 写入文本文件

发布于 2024-12-11 08:28:59 字数 971 浏览 0 评论 0原文

我在试图消磨时间时遇到了一个小问题。我将填写的表格内容写入文本文件,以便于阅读。目前,所有内容都放在一行中,因此我无法判断消息从哪里开始或结束。我写了一个这样的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 技术交流群。

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

发布评论

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

评论(3

黯然#的苍凉 2024-12-18 08:28:59

更改此:

fwrite($fh, $stringData);

与此:

fwrite($fh, $stringData . PHP_EOL);

或:

fwrite($fh, $stringData . "\r\n");

change this:

fwrite($fh, $stringData);

with this:

fwrite($fh, $stringData . PHP_EOL);

or:

fwrite($fh, $stringData . "\r\n");
如此安好 2024-12-18 08:28:59

当您编写平台感知换行符时,您可以附加 PHP_EOL。即 $stringData =; 。 PHP_EOL;

如果不需要平台感知,使用 Windows 换行符是一个安全的选择。

define('CRLF', "\r\n");

$stringData = <string> . CRLF;
fwrite($fh, $stringData);

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.

define('CRLF', "\r\n");

$stringData = <string> . CRLF;
fwrite($fh, $stringData);
吃素的狼 2024-12-18 08:28:59

我不太清楚因为你正在使用。 txt 而不是银行,但我对这种特定情况的建议是使用 json 格式。

示例:

$myFile = "sms_Numbers_Mgs.txt";

$fh = fopen($myFile, 'a+') or die("can't open file");

//assembles a string of type json
$data = json_encode(array(
    'from'    => $_POST[from],
    'friend'  => $_POST[friend],
    'message' => $_POST[message]
));

//Write in the file
fwrite($fh, $data);
fclose($fh);

//reads the file
$content = file_get_contents($myFile);
$object  = json_decode($content);
var_dump($object);

很好,至少组织得更好。

一个拥抱!

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:

$myFile = "sms_Numbers_Mgs.txt";

$fh = fopen($myFile, 'a+') or die("can't open file");

//assembles a string of type json
$data = json_encode(array(
    'from'    => $_POST[from],
    'friend'  => $_POST[friend],
    'message' => $_POST[message]
));

//Write in the file
fwrite($fh, $data);
fclose($fh);

//reads the file
$content = file_get_contents($myFile);
$object  = json_decode($content);
var_dump($object);

good it is at least better organized.

a hug!

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