如何在 Perl CGI 脚本中可靠地确定上传的文件名?

发布于 2024-07-25 02:03:22 字数 122 浏览 10 评论 0原文

从上传表单传递到 Perl CGI(使用 CGI.pm)脚本的文件名取决于客户端计算机,并且可能包含与客户端相关的路径分隔符。 是否有一种可靠的方法来解析传递的参数以确定文件名(通常是最后一个路径分隔符后面的最后一个子字符串)。

The filename passed from an upload form to a Perl CGI (using CGI.pm) script depends on the client machine and may contain client-dependent path separators. Is there a reliable way of parsing the passed parameter to determine the filename (usually the last sub-string following the last path separator).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

梦幻之岛 2024-08-01 02:03:22

我只在客户端使用 MSIE(意味着 Windows 路径)时遇到过路径分隔符的问题。 我使用了一个相当简单的正则表达式来处理这个问题。 但是,您可以扩展正则表达式(甚至拆分)来处理最常见的路径分隔符 - '/'、'\' 和偶尔的 ':'。

或者,您应该能够从 useragent 字符串中计算出文件系统类型(也许使用 HTTP: :DetectUserAgentHTTP::BrowserDetect)。 鉴于您可以在解析文件之前调用 File::BaseName 的 fileparse_set_fstype 函数。

就像是:

use File::Basename;
use HTTP::BrowserDetect;

# ... get the filename into $upload_file and agent into $user_agent_string.

my $browser = HTTP::BrowserDetect->new($user_agent_string);

my $ostype;

$ostype = 'MSWin32' if $browser->windows;
$ostype = 'Unix' if $browser->unix;
# There are more tests available.

fileparse_set_fstype($ostype);

my $filename = basename( $upload_file);

I've only ever experienced the issue with path separators when the client is using MSIE (implying Windows paths). I've used a fairly simple regular expression to handle that. However, you could extend the regular expression (or even split) to handle the most common path separators - '/', '\' and the occasional ':'.

Alternatively, you should be able to work out the filesystem type from the useragent string (perhaps using HTTP::DetectUserAgent or HTTP::BrowserDetect). Given that you could call the fileparse_set_fstype function of File::BaseName before parsing the file.

Something like:

use File::Basename;
use HTTP::BrowserDetect;

# ... get the filename into $upload_file and agent into $user_agent_string.

my $browser = HTTP::BrowserDetect->new($user_agent_string);

my $ostype;

$ostype = 'MSWin32' if $browser->windows;
$ostype = 'Unix' if $browser->unix;
# There are more tests available.

fileparse_set_fstype($ostype);

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