dsepcxw181184853 2011-05-26 15:15
浏览 42
已采纳

PHP,写入文件时换行

I am trying to keep a running total of all the responses to a form I have written, but I am having trouble making it so that each response takes a new line. I have my code down below. I just want it so that it is easier to read because right now what happens is that all the responses are jammed together would like to have each one on a new line. I tried a few things and have them commented in the code and what the result was. Thanks in Advance.

<?php
if (isset($_POST['sometext']))
    {
    $myFile = "testFile.txt";
    $thetext=$_POST['sometext'] ;//added + "
" here but all response turned to 0 
    writemyfile($myFile,$thetext,"a");
    } else
    {
    $thetext="Enter text here";
    }

function readmyfile($thefile)
    {  
        $file = fopen($thefile, "r") or exit("Unable to open file!");
        //Output a line of the file until the end is reached
        while(!feof($file))
        {
            echo fgets($file). "<br />";
        }
        fclose($file);
    }

function writemyfile($thefilename,$data,$mode) 
    {
        $myfile=fopen($thefilename,$mode);
        fwrite($myfile, $data); // added + "
" here and responses turned 0
        fclose($myfile);
    }  
?>
<html>
    <head>
        <title> Zain's Test Site</title></head>
    <body>
        <form method="post" action="<?php echo $php_self ?>">
            <input type="text" name="sometext" value="<?php echo $thetext ?>" >
            <input type="submit" name="Submit" value="Click this button">
        </form>
        <?php readmyfile("testFile.txt"); ?>
    </body>

  • 写回答

5条回答 默认 最新

  • dsa45132 2011-05-26 15:18
    关注

    Can you try appending the newline character ( ) to the $thetext variable like this:

    $thetext=$_POST['sometext'] . "
    ";
    

    Remember to use '.' as the concatenation operator, and use double-quotes around the newline character.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 关于#网络安全#的问题:求ensp的网络安全,不要步骤要完成版文件
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥20 使用Photon PUN2解决游戏得分同步的问题
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM