Jump to content

[SOLVED] Need help using fwrite(); to overwrite a specific line/text in a file


elis

Recommended Posts

I haven't actually written the code for this yet, since I haven't figured out how to do what the title says:

 

but, I have a file "header.php" that contains a line (amongst two hundred other lines)

 

<php require(include.php); ?>

 

I'm trying to use fwrite(); in another file that can alter this line, and only this line, based on what a user enters into a form. However, I can't figure out how to access the specific line in "header.php"

 

I've read of fseek(); on PHP.net and some other sites, but don't quite understand it's purpose or if it would aid in my goal.

 

Could someone recommend how I could go about this?

This should work:


$fileurl = 'file url here';

$search = '<php require(include.php); ?>';
$replace = 'what you want there instead'; 

$file_contents = file_get_contents($fileurl);

$fh = fopen($fileurl, "w");
$file_contents = str_replace($search,$replace,$file_contents);
fwrite($fh, $file_contents);
fclose($fh);

 

To be sure not to overwrite something else, add some specific comment lines to the code. IE: <php require(include.php); /* special */ ?> - that way it's not the same as any other piece of code.

 

Or you could also go by line, but I don't have a handy code ready for that.

Archived

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

×
×
  • 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.