Jump to content

Writing to a file on a specific line


dual_alliance

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
Link to comment
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
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.