Edit txt file remotely

Asked

Viewed 1,041 times

0

I would like to make a page in HTML, 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 the buysingle.html

(In case, I will use the content of the page buysingle.html to carry inside a modal that I created to buy music.)

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

Thank you, from now on.

Code of the text file 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>
  • 1

    Please show us what you have tried and or at least the code of the html page.

  • I still haven’t been able to try anything, because I searched and couldn’t find any code that could do it.

  • I edited the Question and Entered the Code of the HTML Page to be edited by PHP.

1 answer

1

In that case I’ll 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:

  1. Sets a variable to store the file name, called $pagina.
  2. Create a form to display the current content, and then edit the user.
  3. If there was a request in the method POST (if the user clicked save! ) it then checks if there is "content" from POST, if yes, it then selects our file (fopen) in read and write (a+) mode and then writes in this file everything that user typed in the "content" of POST, soon after, closes the file!

To learn more about file manipulation with php, recommend: http://www.samuelcorradi.com.br/manipulando_arquivos_php.html

  • I tested the code, but what it does is "Add".

  • What I want is something that actually edits the code.

  • You’re right, @Hugomarcelo viajei, instead of a+ put w+.

  • I must replace w+ with a+ and it’s solved?

  • I replaced "w+" with "r+". That’s the result I want. Your Code has helped me a lot! Thank you :)

  • You’re welcome, and I’m sorry for the mistakes, I didn’t get to test uhsah....

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.