相关疑难解决方法(0)

file_put_contents,file_append和换行符

我正在编写一个PHP脚本,将数字添加到文本文件中.我想在每一行都有一个数字,如下所示:

1
5
8
12
Run Code Online (Sandbox Code Playgroud)

如果我使用file_put_contents($filename, $commentnumber, FILE_APPEND),结果如下:

15812
Run Code Online (Sandbox Code Playgroud)

如果我添加换行符file_put_contents($filename, $commentnumber . "\n", FILE_APPEND),则在每个数字后面添加空格,在末尾添加一个空行(下划线表示空格):

1_
5_
8_
12_
_
_
Run Code Online (Sandbox Code Playgroud)

我如何获得该功能以我想要的方式添加数字,没有空格?

php line-breaks

33
推荐指数
4
解决办法
7万
查看次数

PHP将文件从输入写入txt

我在这个网站上搜索了一个答案,但找不到任何答案.

我有一个表单,我想将输入的内容写入txt文件.为了简单起见,我只写了一个简单的表单和一个脚本,但它一直让我得到一个空白页面.这就是我得到的

<html>
<head>
    <title></title>
</head>
<body>
    <form>
        <form action="myprocessingscript.php" method="post">
        <input name="field1" type="text" />
        <input name="field2" type="text" />
        <input type="submit" name="submit" value="Save Data">
    </form>
    <a href='data.txt'>Text file</a>
</body>
Run Code Online (Sandbox Code Playgroud)

这是我的PHP文件

<?php
$txt = "data.txt"; 
$fh = fopen($txt, 'w+'); 
if (isset($_POST['field1']) && isset($_POST['field2'])) { // check if both fields are set
   $txt=$_POST['field1'].' - '.$_POST['field2']; 
   file_put_contents('data.txt',$txt."\n",FILE_APPEND); // log to data.txt 
   exit();
}
    fwrite($fh,$txt); // Write information to the file
    fclose($fh); // Close the file
    ?>
Run Code Online (Sandbox Code Playgroud)

php

14
推荐指数
2
解决办法
12万
查看次数

标签 统计

php ×2

line-breaks ×1