PHP Write To Text File

2019-09-10 10:10发布

When I write to text file , all data printed on first line , although I used PHP_EOL , I want to print it line by line.

            $file_Name = './textFilesData/Accounts_Deafult_Data.txt';
            $current = file_get_contents($file_Name);
            $c = 0;

            while ( $c < 5 )
                {
                    $current = 'Rafat';
                    $current = $current.PHP_EOL;    

                    $c++;
                }
            file_put_contents($file_Name, $current);

3条回答
Evening l夕情丶
2楼-- · 2019-09-10 10:35

You can use: $current = "$current1\n";

  $file_Name = './textFilesData/Accounts_Deafult_Data.txt';
            $current = file_get_contents($file_Name);
            $c = 0;

            while ( $c < 5 )
                {
                    $current1 = 'Rafat';
                    $current .= "\n$current1";    

                    $c++;
                }
            file_put_contents($file_Name, $current);
查看更多
\"骚年 ilove
3楼-- · 2019-09-10 10:40

Use this,

$current .= "Rafat";
查看更多
唯我独甜
4楼-- · 2019-09-10 10:54

Adding \r\n should work

$file_Name = './textFilesData/Accounts_Deafult_Data.txt';
            $current = file_get_contents($file_Name);
            $c = 0;

            while ( $c < 5 )
                {
                    $current .= 'Rafat'."\r\n";    

                    $c++;
                }
            file_put_contents($file_Name, $current);
查看更多
登录 后发表回答