php find and replace help

Discussion in 'Web Design and Development' started by fiftyfour123, Jun 25, 2008.

  1. fiftyfour123 macrumors member

    Joined:
    Feb 26, 2008
    Location:
    New York, NY
    #1
    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($handlefilesize($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.
     
  2. angelwatt Moderator emeritus

    angelwatt

    Joined:
    Aug 16, 2005
    Location:
    USA
    #2
    Try this (untested):
    PHP:
    $filename $_POST['filename'];
    $handle fopen($filename"w+");
    $message fread($handlefilesize($filename));
    $data_to_save eregi_replace("phone""error"$message);
    fwrite($handle$data_to_save);
    fclose($handle);
     
  3. Luveno macrumors member

    Luveno

    Joined:
    May 12, 2006
    Location:
    Nova Scotia, Canada
    #3
    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;
    }
    ?>
     
  4. fiftyfour123 thread starter macrumors member

    Joined:
    Feb 26, 2008
    Location:
    New York, NY
    #4
    both scripts return [function.fopen]: failed to open stream: Permission denied in /Library/WebServer/Documents/admin/action.php
     
  5. angelwatt Moderator emeritus

    angelwatt

    Joined:
    Aug 16, 2005
    Location:
    USA
    #5
    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.
     
  6. HomeBru Studios macrumors member

    HomeBru Studios

    Joined:
    Jun 4, 2008
    #6
    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!!
     
  7. fiftyfour123 thread starter macrumors member

    Joined:
    Feb 26, 2008
    Location:
    New York, NY
    #7
    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.
     

Share This Page