简体   繁体   English

我可以从HTML表单中读取输入并将其保存在TXT文件中的特定位置吗?

[英]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作为代码):

  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) (将$path_to_your_file替换$path_to_your_file要更新的文件的相对路径)

  2. 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”)

  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: 根据用户的要求,这是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.

相关问题 我可以使用 JAVASCRIPT/jQuery 将表单中的输入保存到 HTML 中的 .txt,然后使用它吗? - Can I save input from form to .txt in HTML, using JAVASCRIPT/jQuery, and then use it? 我可以使用 javascript 将 HTML 表单数据(包括单选按钮)保存为本地 txt 文件吗? - Can I save a HTML form data (including radio button) as a local txt file using javascript? 我有这个代码,我想从用户那里获取输入,然后将它作为 HTML 输入中的 .txt 文件保存到我的设备本地 - I have this code, I want to take the input from the user and then save it to my device locally as .txt file from HTML Input 如何从一个文件中移动一个特定的文件<input type=file multiple>到一个特定的表格里面? - How can I move one specific file from an <input type=file multiple> to inside a specific form? 如何从 textarea 的值中保存 a.txt 文件? - How can I save a .txt file from the value of a textarea? 如何将数据从.txt文件保存到JavaScript中的数组? - How can i save data from a .txt file to an array in JavaScript? 如何保存html表单输入数据? - How can I save html form input data? 如何防止从html文件下载特定项目? ( <input type=“file”> ) - How can I prevent a specific item from being downloaded from my html file? (<input type=“file”>) 是否可以使用 html 上的输入值保存文本并使用 JavaScript 写入本地 txt 文件? - Is it possible to save texts from input value on html and write on local txt file by using JavaScript? 从txt文件读取-html / css网站 - Read from a txt file - html/css website
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM