如何从 Perl CGI 脚本读取 Web 服务器上的文件,然后打印数据?
我的网络服务器上有一个文件“a.cpm”。我有一个处理程序,当您访问 asdasd.com/a.cpm 时,它会启动 CGI perl 脚本。我尝试读取文件然后打印数据,但它没有执行任何操作。
#!/usr/bin/perl
print "Content-type:text/html\r\n\r\n";
print "test string";
print "<br>";
$filepath = $ENV{'PATH_TRANSLATED'};
open FILE, $filepath or die $!;
my @lines = <FILE>;
while (my $line = <FILE>)
{
print $_;
}
I have a file "a.cpm" on my webserver. I have a handler that when you go to asdasd.com/a.cpm it starts the CGI perl script. I have tried reading the file then printing the data but it doesnt do anything.
#!/usr/bin/perl
print "Content-type:text/html\r\n\r\n";
print "test string";
print "<br>";
$filepath = $ENV{'PATH_TRANSLATED'};
open FILE, $filepath or die $!;
my @lines = <FILE>;
while (my $line = <FILE>)
{
print $_;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您读过 brian d foy 的 如何对 Perl CGI 脚本进行故障排除?并遵循其建议?
Have you read brian d foy's How can I troubleshoot my Perl CGI script? and followed through with its suggestions?
如果您的处理程序工作正常并且您已更改 CGI 脚本的文件权限
chmod a+x
,那么我建议使用 CGI 模块,如下面的代码所示。编辑:污点检查、打开警告和使用严格都是很好的做法,对于 Web 应用程序来说更是如此。
If your handler is working fine and you have changed the file permissions
chmod a+x
of your CGI script, then I suggest using the CGI module as shown in the code below.EDIT: Taint checking, turning on the warnings and using strict are good practice, more so for web applications.
接受的答案并不是开箱即用的——这里有一个细微的变化——只需调整
file.txt
的路径:The accepted answer does not work out of the box -- here is a slight variation that does -- just adjust the path to
file.txt
: