[英]Can I read the input from a HTML form and save it at at a specific position in TXT file?
I used this as reference: Can I save input from form to .txt in HTML, using JAVASCRIPT/jQuery, and then use it? 我以此为参考: 我可以使用JAVASCRIPT / jQuery将表单中的输入保存为HTML中的.txt,然后再使用它吗?
Can I save input from form to .txt in HTML, using JAVASCRIPT/jQuery, and then use it?. 我可以使用JAVASCRIPT / jQuery将表单中的输入保存为HTML中的.txt,然后使用吗? But that is intended for client side and I want server side.
但这是针对客户端的,我想要服务器端的。
I have a TXT file with this content: 我有一个具有以下内容的TXT文件:
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). 我是否可以从HTML表单中读取输入并将其保存在此TXT文件中的特定位置(托管)。 The specific position is INPUT-WORD-HERE.
具体位置是INPUT-WORD-HERE。 So, the idea would be to replace all the INPUT-WORD-HERE with the HTML input.
因此,该想法将用HTML输入替换所有INPUT-WORD-HERE。
So from what you describe, this is what should be done (I used PHP for the code as you tagged your question with it): 因此,根据您的描述,这就是应该做的事情(在您用问题标记了代码时,我使用了PHP作为代码):
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) (将
$path_to_your_file
替换$path_to_your_file
要更新的文件的相对路径)
Replace all the instances of INPUT-WORD-HERE with the new input: 用新的输入替换INPUT-WORD-HERE的所有实例:
$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.
这比起初看起来有些棘手,因为INPUT-WORD-HERE一次到另一次不会是相同的文本。 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 (eg: "example" or "PAGE") 我替换了整个URL,以避免在先前的INPUT-WORD-HERE值与文件中的其他文本匹配时可能出现的问题(例如:“ example”或“ PAGE”)
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: 根据用户的要求,这是HTML表单的简单示例:
<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"
). 您只需要确保文本输入的名称与您在
$_GET
(或$_POST
读取的参数的名称相同,然后就需要将形式更改为method="post"
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.