我提出使用旧技术的解决方案是因为您要求使该解决方案适用于旧文件格式 (html)!就这样吧!不要害怕!我们要做的是使用Server Side Includes (SSI) 通过“#exec cgi”指令将Common Gateway Interface (CGI) program 的结果嵌入到静态HTML 文档中。这听起来很复杂,但其实很简单。我们只需要创建两个文件,然后确保 Web 服务器配置正确。三个简单的步骤!我会一步一步地指导你:
1.创建您的 HTML 文件
您的 HTML 文件内容就是这样(只需使用简单的文本编辑器复制并粘贴到服务器上的 HTML 文件中):
<HTML>
<TITLE>Load a certain line from a .txt into html</TITLE>
Here's the content of line 546 in filename.txt:
<!--#exec cgi="/cgi-bin/perl-print-line.pl 546 filename.txt"-->
</HTML>
2。创建您的 PERL 脚本文件
以上还不行。它依赖于一个简单的程序来输出你的行号。它是perl-print-line.pl by Alvin Alexander,您也可以通过将下面的 PERL 代码复制并粘贴到文本编辑器中来简单地创建此文件(请务必将其放在您的 cgi-bin 目录中):
#!/usr/bin/perl
# purpose: print a specific line from a text file
# usage: perl-print-line.pl line-number input-file
# use perl argv to verify the number of command line arguments
@ARGV == 2 or die "Usage: print-specific-line.pl line-number input-file\n";
$desired_line_number = $ARGV[0];
$filename = $ARGV[1];
# use perl open function to open the file (or die trying)
open(FILE, $filename) or die "Could not read from $filename, program halting.";
# loop through the file with a perl while loop, stopping when you get to the desired record
$count = 1;
while (<FILE>)
{
if ($count == $desired_line_number)
{
# print line, then get out of here
print $_;
close FILE;
exit;
}
$count++;
}
close FILE;
print "Sorry, I think the line number you entered is greater than the number of lines in the file.\n";
3。在您的 Web 服务器上配置 SSI
现在,除非您的网络服务器首先配置为处理 SSI,否则上述所有操作都将不起作用。所有流行的 Web 服务器仍然可以这样做,但您必须首先确保它已配置。根据PerlScriptsJavaScripts.com 网站,
如果你的服务器是 unix/linux 服务器,你可以很确定
支持 SSI。 SI 需要使用 Apache 服务器软件。最多
unix/linux 服务器使用 Apache。一些 Windows 服务器也使用 Apache,但是
他们中的大多数使用 IIS(Internet 信息服务器)。服务器
软件不是你可以改变的,因为它运行
整个服务器(包括其他人的网站)您的主机也将
拒绝为你改变它。相反,他们可能会很友善地搬家
将您的站点转移到他们使用 Apache 的服务器之一。
大多数人会告诉您,要使用 SSI,您必须将页面命名为
“shtml”扩展名。虽然最初可能是这种情况,但您可以设置
服务器也接受和解析常规的“htm”或“html”文件。
如果您使用的是 Windows 服务器而不是 UNIX/Linux 服务器,那么您可能会发现 this article by Robert McMurray 很有用。他说,最重要的是,
在 IIS 6 上默认不启用 SSI。要在 IIS 6 上启用 SSI,请设置
服务器端的状态包括 Web 服务扩展
在 Internet 信息服务 (IIS) 管理器中允许。
默认情况下,IIS 7 上不安装 SSI。要在 IIS 7 上安装 SSI,请参阅
Server Side Include 主题。
#exec 的 cmd 指令在 IIS 5 和 IIS 6 中默认禁用。
要启用 cmd 指令,请参阅Microsoft KB article 233969。
#exec 的 cmd 指令现在对 IIS 7 中的 SSI 文件禁用;
你只能使用 cgi 指令。
最后,如果您正在处理 IIS 7.5 并且您的 *.html 文件中的 SSI 指令没有被自动预处理,请尝试阅读 this post。