PHP Write To Text File

青春壹個敷衍的年華 提交于 2019-12-25 14:01:17

问题


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);

回答1:


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);



回答2:


Use this,

$current .= "Rafat";



回答3:


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);


来源:https://stackoverflow.com/questions/31086327/php-write-to-text-file

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!