Results 1 to 4 of 4

Thread: PHP, reading files, and escaping characters

  1. #1

    Thread Starter
    Fanatic Member Wynd's Avatar
    Join Date
    Dec 2000
    Location
    In a bar frequented by colossal death robots
    Posts
    772

    PHP, reading files, and escaping characters

    I am trying to make a PHP script that lets you edit files from a web page, like Geocities. I can view the file fine, but if I save it all the quotes and backslashes get escaped. How can I prevent this?

    This is the code I am using:
    PHP Code:
    <html>
    <head>
    <title>File editor</title>
    <link rel="stylesheet" type="text/css" href="/main.css">
    </head>
    <body>
    <?
    $dirname = substr(strrchr(getcwd(), "\\"), 1);
    $disp_name = $filename;
    if ($mode == 1) {
        echo "<h3>Editing: " . $disp_name . "</h3>";
        $pFile = fopen($filename, "rb");
        $oldContents = fread($pFile, filesize($filename));
        fclose($pFile);
    ?>
    <form method="post" action="<?=$PHP_SELF?>?filename=<?=$filename?>&mode=2">
    <textarea rows="30" cols="70" name="newContents" wrap="off"><?=htmlspecialchars($oldContents)?></textarea>
    <br><br>
    <input type="submit" value="Save changes">
    <input type="button" value="Reload file" onClick="window.location.href='edit.php?filename=<?=$filename?>&mode=1';">
    <input type="button" value="Return to Index "onClick="window.location.href='index.php';">
    </form>
    <?
    }

    if ($mode == 2)
    {
        $pFile = fopen($filename, "wb");
        fwrite($pFile, $newContents);
        fclose($pFile);
        echo "<h3>Saved: " . $filename . "</h3>";
    ?>
    <form method="post" action="<?=$PHP_SELF?>?filename=<?=$filename?>&mode=2" name="a">
    <textarea rows="30" cols="70" name="newContents" wrap="off"><?=$newContents?></textarea>
    <br><br>
    <input type="submit" value="Save changes">
    <input type="button" value="Reload file"onClick="window.location.href='edit.php?filename=<?=$filename?>&mode=1';">
    <input type="button" value="Return to Index "onClick="window.location.href='index.php';">
    </form>
    <? } ?>
    </body>
    </html>
    Alcohol & calculus don't mix.
    Never drink & derive.

  2. #2
    Conquistador
    Join Date
    Dec 1999
    Location
    Australia
    Posts
    4,527
    $newContents = htmlspecialchars($newcontents);
    fwrite($pFile, $newContents);


    not tested but i think that should work

  3. #3

    Thread Starter
    Fanatic Member Wynd's Avatar
    Join Date
    Dec 2000
    Location
    In a bar frequented by colossal death robots
    Posts
    772
    No, doesn't work
    Alcohol & calculus don't mix.
    Never drink & derive.

  4. #4

    Thread Starter
    Fanatic Member Wynd's Avatar
    Join Date
    Dec 2000
    Location
    In a bar frequented by colossal death robots
    Posts
    772
    No, that doesn't work either.
    Alcohol & calculus don't mix.
    Never drink & derive.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width