PDA

View Full Version : php find and replace help




fiftyfour123
Jun 25, 2008, 10:16 PM
i need help making a simple php script. i don't know php and have been trying for hours to get this to work and i cant seem to do it. what i have is a filename and i want to find and replace a word in the file and then save it. this is the code i have and it doesn't do anything to the file. what am i doing wrong?

it gets the filename from an html form and i echo'ed it and it was the right filename so thats not the problem.

<?
$filename = $_POST["filename"];

$data = fopen($filename, "a+");

$handle = fopen($filename, "r+");

$message = fread($handle, filesize($filename));

$data_to_save = eregi_replace("phone", "error", $message);

fwrite($data,$data_to_save);

fclose($data);
?>

don't laugh at my code, i'm new at this, lol.

Thanks for any help.



angelwatt
Jun 25, 2008, 11:18 PM
Try this (untested):
$filename = $_POST['filename'];
$handle = fopen($filename, "w+");
$message = fread($handle, filesize($filename));
$data_to_save = eregi_replace("phone", "error", $message);
fwrite($handle, $data_to_save);
fclose($handle);

Luveno
Jun 26, 2008, 09:30 AM
This might work as well:


<?
$filename = $_POST["filename"];
$data = file_get_contents($filename);
$data = str_ireplace("phone", "error", $data);
if($handle = fopen($filename, "w+")){
fwrite($handle,$data);
fclose($handle);
} else {
echo 'Unable to open '.$filename;
}
?>

fiftyfour123
Jun 26, 2008, 10:02 PM
both scripts return [function.fopen]: failed to open stream: Permission denied in /Library/WebServer/Documents/admin/action.php

angelwatt
Jun 26, 2008, 10:09 PM
both scripts return [function.fopen]: failed to open stream: Permission denied in /Library/WebServer/Documents/admin/action.php

What are the permissions on the file? From Terminal you can do 'ls -l' (that's a dash "el" not a one) to see the files listed and on the left column it'll show the permissions.

Edit: OK, came up with some new code that is tested. One issue was that I needed to change it from POST to GET. Though not sure how you were running the script so you may need to change it back.
$filename = $_GET['filename'];
$lines = file($filename);
foreach ($lines as $key => $value) {
$lines[$key] = eregi_replace("phone", "error", $value);
}
$handle = fopen($filename, "w+");
foreach ($lines as $k => $v) { fwrite($handle, $v); }
fclose($handle);
Also, Luveno's code works if you switch from POST to GET, at least when I tested. There may still be a permission issue that you can check on.

HomeBru Studios
Jun 26, 2008, 11:52 PM
You are use a $_POST["filename"] to obtain the filename but from what I can tell (or read into your message) you seem to be running this as a command line script.

$_POST[] is returned when you submit a web form with a POST action. Likewise with $_GET[] but in that case, it is when the form action is GET.

Perhaps what you are realy after is to use the argv() command to read what filename you are sending into the script from the command line!?!?!?

Does that help any?

Best of luck - don't give up, PHP is pretty neat!!

fiftyfour123
Jun 27, 2008, 11:46 AM
i figured it out using a file editor i found online. if anyone wants to see it i'll post it here, but its kinda long.