Jeff
Jeff

Reputation: 467

PHP fwrite over itself

I'm working on a little experiment here. I've got a test file that I use to run some code without having to actually log into my app.

At any rate, I had a thought today that I could potentially make the page self-editing and make it quicker for me to do certain menial tasks as I wouldn't even need to open up a text editor for them.

It's mostly working as it sits, but I'm getting a weird thing; I'm hoping either someone just happens to have experience with or can shed some light on.

I won't post the code for the entire file, just the relevant stuff. Here's the basic idea:

index.php:

<?php
if(isset($_POST['file'])) {
  // rewrite ourself
  $test_page = fopen('index.php', 'w');
  fwrite($test_page, $_POST['file']);
  fclose($test_page);
  header('Refresh: 0; ' . $_SERVER['PHP_SELF']);
  exit();
  die(); // we should never get this far...
}
?>
<?php <!-- All the tester stuff goes here --> ?>
<?php <!-- Nothing fancy just a bunch of function calls --> ?>
<!doctype html>
<html lang="en">
  <head>
  </head>
  <body>
    <div>
      <!-- a dump of the functions called in the tester area -->
    </div>
    <div>
      <form method='post'>
        <textarea name='file'>
<?php
$self = fopen('index.php', 'r');
while(!feof($self)) {
  echo fgets($self);
}
fclose($self);
?>
        </textarea>
        <input type="submit" value="Edit/Refresh">
      </form>
    </div>
  </body>
</html>

Now everything is working just great, except that the last 5 HTML tags are not being written to the file; or more accurate, the last 5 HTML tags are not being written into the textarea So when it comes time to save, after the submit, they obviously are not being appended. My theory so far is that once the tag is output by PHP, it obviously closes the text area... so I need to find a way to get that closing tag inside the textarea. I verified this by viewing the source of the page, so I think it's correct.

So I think I just need a way to 'escape' them...

I know it's not a good thing in terms of security, but this is just a local page that I use (it's not available to anyone except the person sitting at my computer). So we don't need to discuss the security issues it obviously may have...

All thoughts appreciated! :)

Upvotes: 1

Views: 255

Answers (3)

Mike
Mike

Reputation: 456

You could do a search and replace for the closing text area tag. The a reverse of that when calling it back again. There are functions to replace all html code but if you want to do just the closing tag I suggest making a custom one using str_replace() or str_ireplace().

Upvotes: 0

jeroen
jeroen

Reputation: 91744

The easiest way to solve your problem would be to replace:

echo fgets($self);

with:

echo htmlspecialchars(fgets($self));

That way you still see the special characters in your text box but they will not be rendered as html.

Upvotes: 3

Wug
Wug

Reputation: 13196

This is a really bad idea because if anything goes wrong, it will blow up and never work again, and if you don't have a backup, it will be gone forever.

This is why people use things like php scripts to dynamically generate web pages without modifying the underlying files. I suggest you just add some settings that are stored to disk somewhere.

Upvotes: 0

Related Questions