Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

fiftyfour123

macrumors member
Original poster
Feb 26, 2008
70
0
New York, NY
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.

PHP:
<?
$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.
 
Try this (untested):
PHP:
$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);
 
This might work as well:

PHP:
<? 
$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;
}
?>
 
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.
PHP:
$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.
 
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!!
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.