PHP写入文本文件

时间:2015-06-27 07:34:55

标签: php text-files

当我写入文本文件时,所有数据都打印在第一行,虽然我使用的是PHP_EOL,但我想逐行打印。

            $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 个答案:

答案 0 :(得分:2)

添加\r\n应该有效

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

答案 1 :(得分:1)

您可以使用: $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);

答案 2 :(得分:1)

使用此,

$current .= "Rafat";