Jump to content

Archived

This topic is now archived and is closed to further replies.

dual_alliance

Writing to a file on a specific line

Recommended Posts

I know how to write to a file.  But how do l actually write to a certain line of a file.

Example:

[b]settings.php[/b]
[code=php:0]// Game Details
$gameName = "namehere";  // Line 3
$gameSubName = "subnamehere";[/code]
<br />
[b]somefile.php[/b]
[code=php:0]$nGameName = $gameName;
$nGameSubName = $gameSubName;

$File = "settings.php";
$Handle = fopen($File, 'w');
$Data = "\$gameName = \"$nGameName\" ;\n ";
fwrite($Handle, $Data);[/code]
<br />
How would l make it so it doesn't overwrite everything but only the information on line 3?

Thanks,

dual_alliance

Share this post


Link to post
Share on other sites
Easiest way would be to read the entire file into an array, where each line is an element of the array. Then you can simply edit/alter/etc the specific element/line and write the array back to file.

Share this post


Link to post
Share on other sites
something like this

[code]
$handle = fopen($filename, "r");
$contents = fread($handle, filesize($filename));
fclose($handle);
[/code]

the variable $contents should be an array, where each element is a line from the file.

if you wanted to replace line 4 in the file with "Its just a test"
[code]
$contents[3] = "Its just a test";
[/code]

then write the array back to the file

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.