如何使用 Perl 按类型过滤上传的文件(例如 .png、.bmp、.jpg)
我正在使用以下代码:
#!/usr/bin/perl -wT
use strict;
use CGI;
use CGI::Carp qw ( fatalsToBrowser );
use File::Basename;
my $query = new CGI;
my $filename = $query->param("photo");
my $user_username = $query->param("text_value");
$CGI::POST_MAX = 1024 * 5000;
my $safe_filename_characters = "a-zA-Z0-9_.-";
my $upload_dir = "/" . $user_username;
if ( !$filename )
{
print $query->header ( );
print "There was a problem uploading your photo (try a smaller file).";
exit;
}
my ( $name, $path, $extension ) = fileparse ( $filename, '\..*' );
$filename = "user_pro_pic" . ".png";
$filename =~ tr/ /_/;
$filename =~ s/[^$safe_filename_characters]//g;
if ( $filename =~ /^([$safe_filename_characters]+)$/ )
{
$filename = $1;
}
else
{
die "Filename contains invalid characters";
}
my $upload_filehandle = $query->upload("photo");
open ( UPLOADFILE, ">$upload_dir/$filename" ) or die "$!";
binmode UPLOADFILE;
while ( <$upload_filehandle> )
{
print UPLOADFILE;
}
close UPLOADFILE;
print $query->header ( );
print "$user_username";
print "<script> location.href='http://google.com/' </script>";
I am using the following code:
#!/usr/bin/perl -wT
use strict;
use CGI;
use CGI::Carp qw ( fatalsToBrowser );
use File::Basename;
my $query = new CGI;
my $filename = $query->param("photo");
my $user_username = $query->param("text_value");
$CGI::POST_MAX = 1024 * 5000;
my $safe_filename_characters = "a-zA-Z0-9_.-";
my $upload_dir = "/" . $user_username;
if ( !$filename )
{
print $query->header ( );
print "There was a problem uploading your photo (try a smaller file).";
exit;
}
my ( $name, $path, $extension ) = fileparse ( $filename, '\..*' );
$filename = "user_pro_pic" . ".png";
$filename =~ tr/ /_/;
$filename =~ s/[^$safe_filename_characters]//g;
if ( $filename =~ /^([$safe_filename_characters]+)$/ )
{
$filename = $1;
}
else
{
die "Filename contains invalid characters";
}
my $upload_filehandle = $query->upload("photo");
open ( UPLOADFILE, ">$upload_dir/$filename" ) or die "$!";
binmode UPLOADFILE;
while ( <$upload_filehandle> )
{
print UPLOADFILE;
}
close UPLOADFILE;
print $query->header ( );
print "$user_username";
print "<script> location.href='http://google.com/' </script>";
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
查看 Data::FormValidator 并专门用于上传 Data::FormValidator::Constraints::Upload 。
Data::FormValidator 是处理 CGI 参数验证的方法。您将把验证逻辑分离到一个配置文件中,并且您的自定义验证代码将缩小到只有几行。
Check out Data::FormValidator and for uploads specifically Data::FormValidator::Constraints::Upload .
Data::FormValidator is the way to go handling CGI params validation. You will separate your validation logic into a profile and your custom validation code will shrink to just few lines.
File::LibMagic 是高级检测器。未经测试:
File::LibMagic is the superior detector. Untested:
在测试无效字符的部分之后,您可以比较文件扩展名的 $filename 后缀。当然,这样做的问题是文件的实际内容可能不匹配(它们通常会匹配,但你不能总是依赖于此)...
if ($filename =~ /.(jpe?g|gif |png|pdf)$/) { # --- 检查 .jpg、.jpeg、.gif、.png 或 .pdf
# --- 支持的文件类型
} 否则{
# --- 不支持的文件类型
modperl_2
如果您需要实际比较基于 MIME 类型检测到的文件内容的类型,那么这将需要更多的处理能力(Apache HTTPd 提供了一个用于检测 MIME 类型的模块,这可能很有用,但您可能还必须升级到 并使用 libapreq2 访问此 API)。
然而,对于大多数目的来说,简单的文件扩展名测试应该没问题。
After the section where you test for invalid characters, you can compare the suffix of $filename for a file extension. Of course, the problem with this is that the actual contents of the file may not match (they usually will, but you can't always depend on this)...
if ($filename =~ /.(jpe?g|gif|png|pdf)$/) { # --- Check for .jpg, .jpeg, .gif, .png, or .pdf
# --- Supported file type
} else {
# --- Unsupported file type
}
If you need to actually compare the type based on MIME-type-detected file contents, then that will require more processing power (Apache HTTPd provides a module for detecting MIME types which might be useful, but you may also have to upgrade to modperl_2 and use libapreq2 to access this API).
For most purposes, however, a simple file extension test should be okay.
查看 File::MimeInfo,请参阅:
https://metacpan.org/pod/File::MimeInfo
Check out File::MimeInfo, see:
https://metacpan.org/pod/File::MimeInfo