Page 2 of 2
Re: Help replacing a whole line in file
Posted: Wed Aug 27, 2008 10:55 am
by xNuManx
i do have them i just wrote that manually.
I tried this:
Code: Select all
<?php
$new = $_POST['newname'];
echo "$new <br /><br />";
$file = file_get_contents('variables.php');
echo "$file <br /><br />";
$file = preg_replace("/\$name = '.*?';/i", "\$name = '$new';", $file);
echo "$file <br /><br />";
file_put_contents('variables.php', $file);
?>
I put new in the input box, then ran the script and it echoed new. but the file is still unchanged.
Re: Help replacing a whole line in file
Posted: Wed Aug 27, 2008 11:01 am
by Chalks
xNuManx wrote:I put new in the input box, then ran the script and it echoed new. but the file is still unchanged.
it didn't echo anything else? If not, then it's not opening the file variables.php correctly (or variables.php is empty). Make sure it's in the same directory and everything.
Re: Help replacing a whole line in file
Posted: Wed Aug 27, 2008 11:32 am
by xNuManx
Chalks wrote:xNuManx wrote:I put new in the input box, then ran the script and it echoed new. but the file is still unchanged.
it didn't echo anything else? If not, then it's not opening the file variables.php correctly (or variables.php is empty). Make sure it's in the same directory and everything.
When i put wrong directory it gives me this error:
Warning: file_get_contents(includes/variables.php) [function.file-get-contents]: failed to open stream: No such file or directory in name_do.php on line 4
so i know its fine

. Is this the only method there is to do this?
Re: Help replacing a whole line in file
Posted: Wed Aug 27, 2008 11:35 am
by Chalks
This is the easiest way to do it.
are you sure it _only_ echoed "new"?
Re: Help replacing a whole line in file
Posted: Wed Aug 27, 2008 11:49 am
by xNuManx
Chalks wrote:This is the easiest way to do it.
are you sure it _only_ echoed "new"?
Yes, im pretty sure. I tried it again and only echoed New then i input hi and it echoed hi. Any suggestions?
Re: Help replacing a whole line in file
Posted: Wed Aug 27, 2008 11:56 am
by Chalks
xNuManx wrote:Yes, im pretty sure. I tried it again and only echoed New then i input hi and it echoed hi. Any suggestions?
Variables.php is empty, or file_get_contents isn't actually getting the content. I suggest opening the file and checking. Unfortunately, I'm leaving work now, so I won't be able to help any further until much later tonight. I've tested the code I gave you though, and as far as I can tell, it should work just fine.
Re: Help replacing a whole line in file
Posted: Wed Aug 27, 2008 1:25 pm
by xNuManx
Chalks wrote:xNuManx wrote:Yes, im pretty sure. I tried it again and only echoed New then i input hi and it echoed hi. Any suggestions?
Variables.php is empty, or file_get_contents isn't actually getting the content. I suggest opening the file and checking. Unfortunately, I'm leaving work now, so I won't be able to help any further until much later tonight. I've tested the code I gave you though, and as far as I can tell, it should work just fine.
Weird, is not empty. Well thanks for trying anyways
