0

I used this as reference: Can I save input from form to .txt in HTML, using JAVASCRIPT/jQuery, and then use it?

Can I save input from form to .txt in HTML, using JAVASCRIPT/jQuery, and then use it?. But that is intended for client side and I want server side.

I have a TXT file with this content:

PAGE=1
until [ $PAGE -gt 9 ]; do
wget "http://www.example.com/INPUT-WORD-HERE/page_0"$PAGE".jpg"
let PAGE+=1
done

PAGE=10
until [ $PAGE -gt 99 ]; do
wget "http://www.example.com/INPUT-WORD-HERE/page_"$PAGE".jpg"
let PAGE+=1
done

Is it possible for me to read the input from a HTML form and save it at a specific position in this TXT file (hosting). The specific position is INPUT-WORD-HERE. So, the idea would be to replace all the INPUT-WORD-HERE with the HTML input.

Community
  • 1
  • 1
PUSTAKAKORAN.COM
  • 455
  • 1
  • 5
  • 18

1 Answers1

1

So from what you describe, this is what should be done (I used PHP for the code as you tagged your question with it):

  1. Open your batch file as a string.

    $fileTXT = file_get_contents($path_to_your_file, FILE_USE_INCLUDE_PATH);
    

    (Replace $path_to_your_file with the relative path to the file that you want to update)

  2. Replace all the instances of INPUT-WORD-HERE with the new input:

    $fileTXT = str_replace("INPUT-WORD-HERE", $textToReplace, $fileTXT);
    

    But it is not that simple. This is a bit trickier than what it seems initially, as INPUT-WORD-HERE will not be the same text from one time to another. We need to create a generic script that will replace whatever the value is:

    $pos1 = strpos($fileTXT, "http://www.example.com/");
    $textFrom = substr($fileTXT, $pos1+23, strpos($fileTXT, "/", $pos1 + 23) - $pos1 - 23);
    $fileTXT = str_replace("http://www.example.com/" . $textFrom . "/", "http://www.example.com/" . $textTo . "/", $fileTXT);
    

    I replace the whole URL, to avoid possible problems if the previous INPUT-WORD-HERE value matched a different text in the file (e.g.: "example" or "PAGE")

  3. Save the file.

    file_put_contents($path_to_your_file, $fileTXT);
    

The final code would look like:

<?php

// here the relative path to your batch file
$filename = "myfile.txt";
// replace $_GET["txt"] for the name of the input that will have the new value. You can use $_POST too
$textTo = $_GET["txt"];

// read the content of the file into a string
$fileTXT = file_get_contents($filename, FILE_USE_INCLUDE_PATH);

// get the word to be replace from the URL (23 is the length of "http://www.example.com/", you'll need to change that number to fit the URL that you place there)
$pos1 = strpos($fileTXT, "http://www.example.com/");
$textFrom = substr($fileTXT, $pos1+23, strpos($fileTXT, "/", $pos1 + 23) - $pos1 - 23);
$fileTXT = str_replace("http://www.example.com/" . $textFrom . "/", "http://www.example.com/" . $textTo . "/", $fileTXT);

// overwrite the content of the file
file_put_contents($filename, $fileTXT);

As I specified in the comments, this solution presents some dangers and should not be used with common users (you said it was Ok, as it was only for you). If you change it so common users can access it, you should preprocess and sanitize the input.


As requested by the user, this is a simple example of how the HTML form could be:

<form method="get" action="YOUR_PHP_FILE_WITH_THE_ABOVE_CODE">
    <label for="txt">
        Write here the new name:
        <input type="text" name="txt" id="txt" />
    </label>
    <input type="submit" value="Make Changes" />
</form>

You just need to make sure that the text input has the same name as the parameter that you read in the $_GET (or in the $_POST, but then you'd need to change the form to method="post").

Alvaro Montoro
  • 28,081
  • 7
  • 57
  • 86