如何从 Perl CGI 脚本读取 Web 服务器上的文件,然后打印数据?

发布于 2024-10-04 18:17:34 字数 368 浏览 4 评论 0原文

我的网络服务器上有一个文件“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

森末i 2024-10-11 18:17:34

Have you read brian d foy's How can I troubleshoot my Perl CGI script? and followed through with its suggestions?

安穩 2024-10-11 18:17:34

如果您的处理程序工作正常并且您已更改 CGI 脚本的文件权限 chmod a+x,那么我建议使用 CGI 模块,如下面的代码所示。

#!/usr/bin/perl
use CGI qw(:standard);
print <<HTML;
Content-type: text/html

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head><title>Path Translated</title></head>
<body>
HTML

$filepath = $ENV{'PATH_TRANSLATED'};
open FILE, $filepath or die $!;
my @lines = <FILE>;
while (my $line = <FILE>) 
{
    print $_;
 }

print <<HTML;   
</body>
</html>
HTML

编辑:污点检查、打开警告和使用严格都是很好的做法,对于 Web 应用程序来说更是如此。

#!/usr/bin/perl -wT
use strict;

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.

#!/usr/bin/perl
use CGI qw(:standard);
print <<HTML;
Content-type: text/html

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head><title>Path Translated</title></head>
<body>
HTML

$filepath = $ENV{'PATH_TRANSLATED'};
open FILE, $filepath or die $!;
my @lines = <FILE>;
while (my $line = <FILE>) 
{
    print $_;
 }

print <<HTML;   
</body>
</html>
HTML

EDIT: Taint checking, turning on the warnings and using strict are good practice, more so for web applications.

#!/usr/bin/perl -wT
use strict;
秋日私语 2024-10-11 18:17:34

接受的答案并不是开箱即用的——这里有一个细微的变化——只需调整 file.txt 的路径:

#!/usr/bin/perl
use CGI qw(:standard);
print <<HTML;
Content-type: text/html

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head><title>Path Translated</title></head>
<body>
HTML

open FILE, "file.txt" or die "could not open filename";
while(<FILE>) {
    print $_;
}
close FILE;

print <<HTML;   
</body>
</html>
HTML

The accepted answer does not work out of the box -- here is a slight variation that does -- just adjust the path to file.txt:

#!/usr/bin/perl
use CGI qw(:standard);
print <<HTML;
Content-type: text/html

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head><title>Path Translated</title></head>
<body>
HTML

open FILE, "file.txt" or die "could not open filename";
while(<FILE>) {
    print $_;
}
close FILE;

print <<HTML;   
</body>
</html>
HTML
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文