0

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
  • are you reading and writing from the same file? Commented Jun 27, 2015 at 7:45

3 Answers 3

2

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);
Sign up to request clarification or add additional context in comments.

Comments

1

Use this,

$current .= "Rafat";

Comments

1

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

4 Comments

I Used it , but still the same @Ahosan Karim Asik
some editor new line text showed as a single line. example windows default notepad views as single line. but have a new line into the text file. to see actual view u can use notepad ++
Yes , This is true . Thanks @Ahosan Karim Asik
@Rafat Haj Ali or u can use \r\n for new line. but to explode line remember that..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.