如何自动将表单生成的文本复制到PHP中的另一个文件中?

如何自动将表单生成的文本复制到PHP中的另一个文件中?

问题描述:

So on my site, I have a form set up, and when one enters information, it spits back another page with the results all neatly formatted in about 10 lines. How can I make PHP copy those 10 lines and append it to the end of another file on my site? If this is only possible in JavaScript, could you please tell me so, so that I may post in the Javascript forum?

Let me provide a link to my website to illustrate: please visit new/entry.hostei.com (DELETE THE /), and click "Submit Query" at the bottom. You need not type anything in the boxes. Viewing the page source, the lines I would want copied are the line starting after < /head>, through the next ten lines (until the blank space starts).

Note: I do not want to replace the "destination file," but merely add lines of code onto the end.

I've tried to search for this on Google, but it involves too many keywords and so there is not much useful output. I've also asked on another forum, but so far they have not been able to provide any useful output.

所以在我的网站上,我有一个表单设置,当一个人输入信息时,它会回吐另一个页面 结果全部格式化为大约10行。 如何让PHP复制这10行并将其附加到我网站上另一个文件的末尾? 如果这只能在JavaScript中使用,请你告诉我,以便我可以在Javascript论坛发帖? p>

让我提供一个链接到我的网站来说明:请访问新的 /entry.hostei.com(删除/),然后单击底部的“提交查询”。 您无需在框中键入任何内容。 查看页面源,我想要复制的行是&lt; / head&gt;,通过接下来的十行(直到空格开始)。 p>

注意:我不想替换“目标文件”,而只是添加代码行到 结束。 p>

我试图在谷歌上搜索这个,但它涉及太多的关键字,因此没有太多有用的输出。 我还问过另一个论坛,但到目前为止他们还没能提供任何有用的输出。 p> div>

How can I make PHP copy those 10 lines and append it to the end of another file on my site?

Easily. Given $data is an array of the lines to add, and $filename is the name of the file being appended to:

// Open the file in Append mode, with the file pointer placed at the end of the file.
// The file will be created if it does not exist.
    $fh = fopen($filename, 'a+');
// Establish a lock on the file.
    flock($fh, LOCK_EX);
// Write each line in the array and a newline.
    foreach($data as $line) {
        fwrite($fh, $line);
        fwrite($fh, "
");
    }
// Expressly release the lock and close the file.
    flock($fh, LOCK_UN);
    fclose($fh);

If your $data is a string instead of an array,

// Open the file in Append mode, with the file pointer placed at the end of the file.
// The file will be created if it does not exist.
    $fh = fopen($filename, 'a+');
// Establish a lock on the file.
    flock($fh, LOCK_EX);
// Write the data and a newline.
    fwrite($fh, $data);
    fwrite($fh, "
");
// Expressly release the lock and close the file.
    flock($fh, LOCK_UN);
    fclose($fh);

Also,

If this is only possible in JavaScript, could you please tell me so

Quite the opposite, Javascript does not have access to the filesystems of either your server or your client's system.

You might want to try file_put_contents:

file_put_contents( "filename.txt", $line, FILE_APPEND );

This would be placed inside of your PHP file that outputs the results to the screen, and you will need to replace "$line" with the variable that holds the line that is being output. Change "filename.txt" to whatever the text file you want the lines to be added to. Any valid filename will work.

Depending on how your code is written, there may be other things involved. You could keep it very simple and do a separate file_put_contents for each line you are dealing with:

file_put_contents( "filename.txt", $line1, FILE_APPEND );
file_put_contents( "filename.txt", $line2, FILE_APPEND );
file_put_contents( "filename.txt", $line3, FILE_APPEND );
...etc...

Where the above, $line1, $line2, $line3 is replaced by the variable name of the output on your result page.

If the line outputs are in a loop, you will probably just need file_put_contents once as in my first example at the top. Again, it all depends on some of the details of your existing code.

Another thing to look out for, you may need to add newlines ( ) at the end of the lines you write to the disk. This may be needed to keep your lines from appearing all-together on the same line in your output file:

file_put_contents( "filename.txt", $line . "
", FILE_APPEND );

See here for more info on file_put_contents: http://www.php.net/file_put_contents