Edit txt file remotely

0

I would like to make an HTML page, and use a PHP page to edit the text of the HTML page (as if it were a Frontend ).

Is there any PHP code for this?

In my example, the HTML page to be edited is buysingle.html

(In this case, I'm going to use the contents of the buysingle.html page to load within a modal that I created to buy the song.)

I wanted a PHP page that had the HTML page code always inside a text box, and that I could edit and save.

Thank you in advance.

Text file code to be edited:

    <div id="albumtrack1">
    <a style="color:lightgrey; text-decoration: none; -webkit-touch-callout: none;
    -webkit-user-select: none;
    -khtml-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    user-select: none;"><br>
            Buy "I Don't Wanna Your Love"
          </a><br>
       <p style="color:grey; text-decoration: none; -webkit-touch-callout: none;
    -webkit-user-select: none;
    -khtml-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    user-select: none;">You can not end this purchase. Music was not yet released.</p>
</div>
<div id="albumtrack2">
    <a style="color:lightgrey; text-decoration: none; -webkit-touch-callout: none;
    -webkit-user-select: none;
    -khtml-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    user-select: none;"><br>
            Buy "Pure"
          </a><br>
<p style="color:grey; text-decoration: none; -webkit-touch-callout: none;
    -webkit-user-select: none;
    -khtml-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    user-select: none;">You can not end this purchase. Music was not yet released.</p>
</div>
<div id="albumtrack3">
    <a style="color:lightgrey; text-decoration: none; -webkit-touch-callout: none;
    -webkit-user-select: none;
    -khtml-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    user-select: none;"><br>
            Buy "Sleep Close to Me"
          </a><br>
<p style="color:grey; text-decoration: none; -webkit-touch-callout: none;
    -webkit-user-select: none;
    -khtml-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    user-select: none;">You can not end this purchase. Music was not yet released.</p>
</div>
    
asked by anonymous 01.05.2015 / 16:54

1 answer

1

In this case I will create a file: editar.php

<?php
  $pagina = "buysingle.html";
  if(isset($_POST)){
    if($_POST["conteudo"]){
      $fopen = fopen($pagina,"w+");
      fwrite($fopen,$_POST["conteudo"]);
      fclose($fopen);
    }
  }
?>
<h1>Editar a pagina: <?= $pagina; ?></h1>
<form method="post">
  <textarea name="conteudo"><?= file_get_contents($pagina); ?></textarea>
  <input type="submit" value="Salvar"/>
</form>

Explaining:

  • Defines a variable to save the file name, called $pagina .
  • Creates a form to display the current content, for which the user can edit.
  • If there was a request in the POST method (if the user clicked save!) then it checks to see if there is "content" of POST , if so, then it selects our file (fopen) in read and write (a +) and then write in this file everything the user typed in the "content" of POST , then close the file!
  • To learn more about file manipulation with php, I recommend: link

        
    01.05.2015 / 17:48