0

I want to write on line 5 of my file.

I can't use replace because my file use many numbers in lines. So I need to know how can modify line 5.

You know we can read line 5 of file with:

$File='myfile.txt';

$readline = file($File);

$line5=$readline[4];'

I need to code that can modify only line 5 of $File.

OneOfOne
  • 95,033
  • 20
  • 184
  • 185
Amin Maleki
  • 156
  • 2
  • 4
  • 19

1 Answers1

2

In your code $read is undefined. It should be $readline, the following should work for you:

$File = 'myfile.txt';
$lines = file($File);
$lines[4] = 'modified line';
file_put_contents($File, implode("\r\n", $lines));
meda
  • 45,103
  • 14
  • 92
  • 122